0

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;



}

Edited by jvasher

2
Contributors
1
Reply
13
Views
4 Years
Discussion Span
Last Post by deceptikon
0

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

test2() : mytest1("test", 6)
{
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.