Question about the process of creating a class - Inheritence

Pages: 12
@Framework,

Oh yeah. Just copied and pasted, didn't really think about updating it. Thanks for all the help everyone.
Never mind, the problem remains. If the constructors are public, but the default arguments do not, the constructor will still be called.

http://ideone.com/A3GBN
closed account (zb0S216C)
I've said this about 3 times now: You've overloaded the default constructor, which means the compiler will not generate one. The constructor you specified requires an argument. Since the compiler is unable to determine the argument of the base-class constructor, the derived class must specify the argument for the constructor. For example:

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
28
29
30
class X1
{
public:
    X1(int arg_value1) : value1(arg_value1) {}
    int value1;
};
 
class X2 : public X1
{
public:
    X2() : X1(0) { } // <-- Here
    int value2;
};

//
// OR:
// 

class X1
{
public:
    X1(int arg_value1 = 0) : value1(arg_value1) {}
    int value1;
};
 
class X2 : public X1
{
public:
    int value2;
};

Wazzak
Last edited on
Oh okay. I was confused by this statement.

If you specify default arguments for all parameters in a constructor, the constructor will act as a default constructor.
closed account (zb0S216C)
It's really not all that complicated. Here's an example that I hope will clarify:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct ObjectA
{
    ObjectA(); // Default constructor
};

struct ObjectB
{
    ObjectB(int); // Not a default constructor
};

struct ObjectC
{
    ObjectC(int A = 0); // Default constructor
}

int main()
{
    ObjectA NewA;    // Calls ObjectA::Object()
    ObjectB NewB;    // Cannot call ObjectB::ObjectB() because ObjectB::ObjectB(int) exists
    ObjectB NewC(1)  // Calls ObjectB::ObjectB(int)
    ObjectC NewD;    // Calls ObjectC::ObjectC(int) with the default argument
    ObjectC NewE(1); // Calls ObjectC::Object(int)
}

Wazzak
Last edited on
Yeah, I was just getting mixed up with the meaning of the default constructor. I thought it meant the constructor that automatically called on creation if there is no explicit call. Turns out it means a constructor that can be called with no arguments, which makes sense in your cases.

So in the last piece of code, the second one is the constructor that is automatically called (because there is no other constructor that takes no arguments), but it cannot be called because of the incorrect parameter.
closed account (zb0S216C)
Yes. Since ObjectB's trivial default constructor (the constructor that takes no arguments) has been overloaded, ObjectB must be given an int in order for it to construct an object. However, you give the int parameter a default argument (like the one in ObjectC), you don't need to pass an int when you construct an object because the compiler will use the default int.

Wazzak
Topic archived. No new replies allowed.
Pages: 12