|
-
May 4th, 2010, 07:22 AM
#1
Constructor Inheritance
How we conclude that the constructors are not inherited??
Because I can call the base class constructors like below which means that the the constructor is accissible .
Code:
class Base{
public:
Base(int x,int y){}
};
class Derived : public Base{
public:
Derived(int a,int b):Base(a,b) //I am calling the Base constructor
{
}
};
Thanks
Last edited by Rajesh1978; May 4th, 2010 at 08:39 AM.
-
May 4th, 2010, 09:31 AM
#2
Re: Constructor Inheritance
make the base constructor private
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 4th, 2010, 09:37 AM
#3
Re: Constructor Inheritance
The base constructor is accessible, yes, but it is not inherited because this will not work:
Code:
class Base{
public:
Base(int x,int y){}
};
class Derived : public Base{
// no constructor, so the default one will be generated
};
int main()
{
Derived(5,6);// compile error, no such constructor for Derived.
}
-
May 4th, 2010, 09:59 AM
#4
Re: Constructor Inheritance
 Originally Posted by Rajesh1978
How we conclude that the constructors are not inherited??
Because I can call the base class constructors like below which means that the the constructor is accissible .
Code:
class Base{
public:
Base(int x,int y){}
};
class Derived : public Base{
public:
Derived(int a,int b):Base(a,b) //I am calling the Base constructor
{
}
};
Thanks
The commented line should be: i am passing constructor's arguments
I'm sure that is called: "Passing constructor's arguments" (or something similar), that's not a actual function call of the constructor, if you need to explicit call the constructor you have a bigger problem with the design of class or class hierarchy.
The constructor of base class is called even if you don't pass parameters (the default c-tor or with default parameters), so you can write: Derived(int a,int b){} // and do something in the c-tor body, of course you need default base c-tor for that.
LE: That line actually says: When construct the "Base part" of Derived object run the Base constructor with the two parameters of the Derived constructor.
Last edited by Zlatomir; May 4th, 2010 at 10:03 AM.
-
May 4th, 2010, 10:15 AM
#5
Re: Constructor Inheritance
In a similar line, I can give another example:
Code:
class Base
{
public:
void ConstructorMethod(int);
};
class Derived:public Base
{
public:
void ConstructorMethod(int,int);
};
int main()
{
Derived d;
d.ConstructorMethod(2,4);
// this is NOT possible
d.ConstructorMethod(2);
}
The second definition of ConstructorMethod hides the original definition. Same thing happen with CTORs - they are always defined, implicitly or explicitly. The derived definition hides the base' version. To call the base class' you can:
Code:
d.Base::ConstructorMethod(2);
And this is also possible!
Code:
class Base {
public: Base(int = 0); // Default just for brevity
};
class Derived{};
...
Derived d;
d.Base::Base(10); // YES - you can call.
This way you can say that constructors are inherited, but just hidden!
-
May 4th, 2010, 10:51 AM
#6
Re: Constructor Inheritance
Sorry Ajay, you are wrong. The standard says constructors cannot be called like that. Comeau rejects this code correctly, tho MSVC10 compiles it without a problem (compiler bug?)...
Code:
struct Base
{
Base( int x ) : X( x )
{}
int X;
};
struct Derived : Base
{
Derived() : Base( 5 )
{}
};
int main()
{
Derived can_do;
can_do.Base::Base(10);
return 0;
}
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 18: error: a constructor or destructor may not have its address
taken
can_do.Base::Base(10);
^
1 error detected in the compilation of "ComeauTest.c".
Constructors are not hidden, nor inherited. Constructors, destructor and copy assignment operators are never inherited. To do so would imo go against the whole concept of type-safety.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 4th, 2010, 11:14 AM
#7
Re: Constructor Inheritance
 Originally Posted by Russco
Constructors, destructor and copy assignment operators are never inherited. To do so would imo go against the whole concept of type-safety.
hmm... a search for "not inherited" only brings up a sentence in C++03 that confirms that destructors are not inherited. Where does the standard state this more specifically? In particular, does it really state that copy assignment operators are not inherited?
-
May 4th, 2010, 11:17 AM
#8
Re: Constructor Inheritance
The standard states that constructors do not have a name, so therefore cannot be explicitly called by name. I'm not sure if it mentions that they are not inherited, but for sure TCPPPL does and we know who wrote that . It would go totally against type safety for constructors, destructors or copy assignment to be inherited. They must be accessible to the derived class but they are not members of the derived class which they would be had they been inherited.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 4th, 2010, 11:44 AM
#9
Re: Constructor Inheritance
Actually I may be wrong about copy assignment. I checked TCPPPL and cant find the info, so I checked Thinking in C++ and thats where this info was stated but I think its wrong because assignment can be virtual so assignment must be inherited and hidden though virtual copy assignment doesn't make much sense to me, but constructors on the other hand I dont think are inherited at all, all they need to be is accessible to the derived class in the same way destructors are.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 4th, 2010, 11:54 AM
#10
Re: Constructor Inheritance
 Originally Posted by Russco
Sorry Ajay, you are wrong. The standard says constructors cannot be called like that.
Look closer: He's not calling constructors. Just normal methods called ConstructorMethod(). Slightly confusing, perhaps, but perfectly fine.
-
May 4th, 2010, 12:07 PM
#11
Re: Constructor Inheritance
 Originally Posted by Lindley
Look closer: He's not calling constructors. Just normal methods called ConstructorMethod(). Slightly confusing, perhaps, but perfectly fine.
Not necessarily confusing. It's called the named constructor idiom. It is almost completely free from a performance point of view, and can be quite useful when constuctor arguments are not quite enough to set the constructors apart, or to really convey the nuances between the different constructors.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 4th, 2010, 12:13 PM
#12
Re: Constructor Inheritance
 Originally Posted by Lindley
Look closer: He's not calling constructors. Just normal methods called ConstructorMethod(). Slightly confusing, perhaps, but perfectly fine.
Oh really?
Code:
d.Base::Base(10); // YES - you can call.
This is what I was talking about specifically. This is not allowed. When constructors are called by a programmer, its implicitly or as part of a type conversion which is still an implicit call. Explicitly calling a constructor is what compilers do, not programmers.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 4th, 2010, 12:19 PM
#13
Re: Constructor Inheritance
 Originally Posted by Russco
Sorry Ajay, you are wrong. The standard says constructors cannot be called like that. Comeau rejects this code correctly, tho MSVC10 compiles it without a problem (compiler bug?)...
Not a bug. It compiles in VC6, VC7, VC7.1, VC8, VC9 and VC10 as well. I do not know about standard as such, and never used Comeau compiler.
Constructors are not hidden, nor inherited. Constructors, destructor and copy assignment operators are never inherited. To do so would imo go against the whole concept of type-safety.
I can take that only for CTOR. Destructor ARE inherited, otherwise virtual destructor wouldn't make sense. Also destructors can be called explicitly using X::~X() syntax.
Also, assignment operator are inherited as you mentioned.
Again I don't know about your compiler.
Anyway, my point was not about standard - but basic concept behind CTOR inheritance!
Last edited by Ajay Vijay; May 4th, 2010 at 12:24 PM.
Reason: messed up text!
-
May 4th, 2010, 12:22 PM
#14
Re: Constructor Inheritance
 Originally Posted by Ajay Vijay
In a similar line, I can give another example:
Code:
class Base
{
public:
void ConstructorMethod(int);
};
class Derived:public Base
{
public:
using Base::ConstructorMethod; //monarch_dodra: add this line here
void ConstructorMethod(int,int);
};
int main()
{
Derived d;
d.ConstructorMethod(2,4);
// this is NOT possible
d.ConstructorMethod(2);
}
The second definition of ConstructorMethod hides the original definition. Same thing happen with CTORs - they are always defined, implicitly or explicitly. The derived definition hides the base' version.
If you add "using Base::ConstructorMethod" inside Derived allows you to not hide the original definition, and write
Code:
// this is NOW possible
d.ConstructorMethod(2);
The whole hiding thing is there more as a "safety mechanism" than a real feature anyways. This is because it is usually not really recommended to re-define a function in a derived class, and forcing the user to write the using declaration is like forcing him to say "Yes, I know what I am doing, and I take full responsabilty"
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 4th, 2010, 12:29 PM
#15
Re: Constructor Inheritance
 Originally Posted by Ajay Vijay
I can take that only for CTOR. Destructor ARE inherited, otherwise virtual destructor wouldn't make sense. Also destructors can be called explicitly using X::~X() syntax.
As I mentioned earlier:
 Originally Posted by C++03 Section 10.3 Paragraph 4
Even though destructors are not inherited, a destructor in a derived class overrides a base class destructor declared virtual; see 12.4 and 12.5.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|