-5

I have a vector list with "registered" classes.

main.cpp

// main.cpp:
// from namespace::class3
bool successfulregistered = RegisterComponents(vector<CustomNamespace::class1,CustomNamespace::class2>);

class3.h

// in class3
    private:
        vector<CustomNamespace> Objectinstance;

    public:
        bool RegisterComponents(vector<CustomNamespace>& RegisterComponents);

class3.cpp

        // implementation
        bool class3::RegisterComponents(vector<CustomNamespace>& RegisterComponents)
        {
            for(int i = 0; i < RegisterComponents.end(); i++)
            {
                class3::Objectinstance->iterator(*RegisterComponents);
                // and then some checks
            }
        }

Now I like to gather access to the classes and created object instances to call methods:

void class3::startserver(void)
{
    for(auto i = Objectinstance.begin(); i != Objectinstance.end(); i++)
    {
        /*  How can I create the objects from the vector list with
                their classes and call the specific constructor? */

        /* i == CustomNamespace::class1 */


    }
}

class1.h

ConfigWatchdog(string &SetJSONFile, const char &cJSONRoot);

class2.h

ServerNetworking(unit& setIPAddress, ...);
  • 1
    The question is unclear and the code does not look like compilable C++ (it confuses iterators and indices, uses `namespace` as type name, etc.) Please clarify your question and tighten up the code to help us answer the question. – user4815162342 Dec 13 '15 at 13:30

1 Answers1

4

Despite your code giving me a WTF moment, I think I can help. i in the loop is an iterator:

for(auto i = Objectinstance.begin(); i != Objectinstance.end(); i++)

You access an element by dereferencing i - *i. You can copy-construct another Namespace (make it capital N, namespace is a keyword, don't you know?):

Namespace obj1 = *i;

And maybe just grab a book.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • But in the list are two difference classes, how can I make a difference? vector; so while I'm iterating with i, it has in each continuity another class, which I just like to call. – Thraax.Session Dec 13 '15 at 14:30
  • `std::vector` can have elements only of one type... The second template parameter is a custom allocator. [Read the reference](http://en.cppreference.com/w/cpp/container/vector). – LogicStuff Dec 13 '15 at 14:33
  • I edited a bit my example to make it more clear with my namespace. Sorry for confusing. :) So `std::vector< mynamespace >Listwithmycustomclasses` may be a list with one type of elements, or not? – Thraax.Session Dec 13 '15 at 14:41
  • Wuhu ok that's already good to know. But now it's still unclear how I can access each class inside and call it with an iterator. Could you please edit your example to make it clear to me? – Thraax.Session Dec 13 '15 at 14:45
  • You have to edit your question first - I don't know what `Namespace` class looks like. – LogicStuff Dec 13 '15 at 14:47
  • I added the constructors from my two classes. Does it help you to understand? They are also in `CustomNamespace` – Thraax.Session Dec 13 '15 at 14:55
  • Not really. Is `Namespace` even a class? Are those two classes nested inside it? – LogicStuff Dec 13 '15 at 14:59
  • Yes they are both nested in the namespace! `Namespace` is not a class. It's a namespace which I use for my classes. They shall be saved in vector, which I like to iterate through and call the classes then. – Thraax.Session Dec 13 '15 at 15:00
  • Well, you can't have `std::vector` then. You'll need one `std::vector` and one `std::vector`. It's totally unclear what you want to do... rethink your problem. – LogicStuff Dec 13 '15 at 15:06
  • So does it not even work if `ConfigWatchdog` and `ServerNetworking`are in same `namespace`? I just want to save classes with same namespace in a list. And iterate through the list later, and create an object by each class which I have saved in the list. :( This is not possible? – Thraax.Session Dec 13 '15 at 15:09
  • `ConfigWatchdog` is totally different from `ServerNetworking`, you really better read some books. – LogicStuff Dec 13 '15 at 15:11
  • Yes it's clear they are different, cuz they are different classes as I wrote. My question wasn't about to save **same** classes, it was all time about different classes. Else I wouldn't have this problem, right?;) `vectorAlistwithdifferentclasses` So is it possible and if yes how and if no, why? – Thraax.Session Dec 13 '15 at 15:15
  • There already questions about that - http://stackoverflow.com/questions/17734042/creating-a-vector-that-holds-two-different-data-types-or-classes. – LogicStuff Dec 13 '15 at 15:17