jeudi 20 décembre 2018

How to use string as an argument for if statement?

So I have a problem. My objective is to iterate through sigly linked list I've created. I want to use std::string as an argument to if statement like listed below. I know it's not a good idea, even more: I know it may be the WORST possible way to do this. It doesn't matter I want to know if it can be done. I tried using macro function but either I'm doing it wrong or it's not possible using such macro. Help me please. I'm out of ideas...

Similar macros I used in my old project where I've passed as parameter a name of function (user typed the name of such function like 'sin' or 'tan') from cmath lib to calculate integral for given function, so I thought it would work in this case as well but apparently not. It says:

conditional expression of type 'std::string' is illegal

//structures.h    
struct node
{
    int _value;
    node *pNext;
};


//functions.cpp
#define retarded_at(x) x
#define retarded_stringify(y) retarded_at(y)

//@param pHead pointer to first element of the list
void retDispIter(node *pHead)
{
    std::string at{ "pHead->pNext" };

    while (retarded_at(at))
    {
        at += "->pNext";
        //here send node->_value to std::cout
    }
}

Inside first while loop "at" would be 'pHead->pNext' poining at next element. With 8 elements in the list the last while would be like:

while(pHead->pNext->pNext->pNext->pNext->pNext->pNext->pNext->pNext)

return false and "at" would contain a 'pointer' (nullptr) to the non-existing 9th element.

Aucun commentaire:

Enregistrer un commentaire