Inherited constructor gets confused with copy constructor

If I uncomment the line below I get this error message in VC++ 2008:

error C2664: 'TestObject3::TestObject3' : cannot convert parameter 1 from 'int' to 'const TestObject3 &'
1> Reason: cannot convert from 'int' to 'const TestObject3'
1> No constructor could take the source type, or constructor overload resolution was ambiguous


Seems to be trying to use the default copy constructor when I want it to use the overloaded constructor from the base class.

Wat do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class TestObject2
{
public:
	TestObject2 (int s)
	{
		s = size;
	}
private:
	int size;
};

class TestObject3 : public TestObject2
{
};

int main (int argc, char **argv)
{
	TestObject2 *NewTest2 = new TestObject2 (8);
//	TestObject3 *NewTest3 = new TestObject3 (8);
	return 0;
}
Overloaded constructor???? Where did you learn that from??
From this tutorial http://cplusplus.com/doc/tutorial/inheritance/#inheritance

It says

What is inherited from the base class?
In principle, a derived class inherits every member of a base class except:

* its constructor and its destructor
* its operator=() members
* its friends



Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.

If the base class has no default constructor or you want that an overloaded constructor is called when a new derived object is created, you can specify it in each constructor definition of the derived class:


derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
Oops - my bad.
I was taking my cue from his post.
You can't. What if I had:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct base {
    int i;
    base(int x) {
        i = x;
    }
};

struct derv : public base {
    std::string str;
}

int main() {
    base* ptr = new derv(10); //uh oh, derv.str is never constructed
}
Hi,
I tried compiling it in linux using g++ compiler.
I received errors different from above but as you can see they are more explainatory
--- without uncommenting :
error: base `TestObject2' with only non-default constructor in class without a constructor

-- with uncommenting the line :
test.cpp:13: error: base `TestObject2' with only non-default constructor in class without a constructor
test.cpp: In function `int main(int, char**)':
test.cpp:19: error: no matching function for call to `TestObject3::TestObject3(int)'
test.cpp:13: note: candidates are: TestObject3::TestObject3(const TestObject3&)

Firstly u need to provide the base class a default constructor. u can do that by either providing a default argument in the constructor you have written or by adding another constructor with no arguments.
Secondly, as you can see there's no constructor for the derived class. When a object of derived class is created , its the responsibility of derived constructor to first call the base class constructor.
Here derived class has no constructor which will take an integer argument to construct an object. So until and unless you provide a constructor to the derived class, the code will not work.
Also ,
After providing a base class constructor a default constructor , you can create a object of derived class like
TestObject3 ob;
This will work fine because default constructor for base class has already been provided and the default constructor of the derived class will be generated by the compiler.
but if you want to do
TestObject3 ob (8) ;
then there must be proper overloaded constructor present which the compiler can call in this case. so a working code can be written like =>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class TestObject2
{
public:
	TestObject2 (int s=0)  // default constructor
	{
		s = size;
	}
private:
	int size;
};

class TestObject3 : public TestObject2
{
	public:
		TestObject3(int s = 1) // default constructor
		{
		}
};

int main (int argc, char **argv)
{
	TestObject2 *NewTest1 = new TestObject2 (8);
	TestObject2 *NewTest2 = new TestObject2;
	TestObject3 *NewTest3 = new TestObject3 (8);
	TestObject3 *NewTest4 = new TestObject3;
	return 0;
}
Last edited on
Thanks, it does indeed say this the tutorial, I must have thought I saw it done this way somewhere else.

Ended up using this method:

derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

..as it seems to be closest to what I want.
Topic archived. No new replies allowed.