Tried searching for an answer to this, but not sure even how to explain this question so i will show a code example.(I blame java, better then saying i'm brain damaged)

I have no problems with pointers. But can't seem to remember the correct way to call the constructor when init on stack.

<&hair pull, hair pull.

class test1 {
public:
    std::string somestring;
    int somevalue;

    test1(std::string ss, int value) {
        somestring = ss;
        somevalue = value;
    }
}


class test2 {
public:
    test2() {
        mytest1("test",6); // <<< that doesn't work
        mytest1 = test1("test",6); // << tried this

        pMytest1 = new test1("test", 4); // << this works, but places it on heap
    }

private:
    // test1 mytest1("test",6); <<< that is not correct.. 
    test1 mytest1;
    test1 *pMytest1;



}

In this case you need to use the constructor's initialization list because there's no default constructor for test1:

test2() : mytest1("test", 6)
{
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.