473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

overloaded constructors and inheritence

86
suppose i have...

Expand|Select|Wrap|Line Numbers
  1.  
  2. class base
  3. {
  4. public:
  5.          base();
  6.          base( char*);
  7.  
  8.          char* return_string() { return string; }
  9. private:
  10.          char[10] string;
  11. };
  12.  
  13. class derived:public base
  14. {
  15. public:
  16.             derived();
  17.             derived(char* , int);
  18.  
  19.             int returnValue( ) { return value; }     
  20.  
  21. private:
  22.          int value;
  23. };
  24.  
  25.  
now when i define things....

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. base::base()
  4. {
  5. memset(string, 0, 10);
  6. }
  7.  
  8. base::base(char* temp)
  9. {
  10.      strcpy(string, temp);
  11. }
  12.  
  13. derived::derived()
  14. {
  15. }
  16.  
  17. // my question is here...this works
  18. derived::derived(char* temp, int number):base(temp);
  19. {
  20.     value =number;
  21. }
  22.  
  23. // but this won't work
  24. derived::derived(char* temp, int number)
  25. {
  26.     base(temp);
  27.     value =number;
  28. }    
  29.  
  30.  
May 20 '07 #1
10 1497
AdrianH
1,251 Expert 1GB
Use strncpy() not strcpy(). It will cause you greif.

You can only invoke the constructor at creation. Once you have entered the body, it has already been invoked. The second example you gave is a Javaism. Can't do that in C++.


Adrian
May 20 '07 #2
ayan4u
86
Use strncpy() not strcpy(). It will cause you greif.

You can only invoke the constructor at creation. Once you have entered the body, it has already been invoked. The second example you gave is a Javaism. Can't do that in C++.


Adrian
thats what i am asking why not....why the second example is not working just like the first....
May 20 '07 #3
AdrianH
1,251 Expert 1GB
thats what i am asking why not....why the second example is not working just like the first....
Because C++ defines it that way. ;)


Adrian
May 20 '07 #4
JosAH
11,448 Expert 8TB
thats what i am asking why not....why the second example is not working just like the first....
Now your question boils down to: why isn't "fietsbandventielnippeltje" an English
word? Because it isn't; C++ wasn't designed that way; objects are constructed
from the inside out like onion layers; an outer layer doesn't exist until an inner
layer is fully constructed.

Destruction works the other way around: from the outer layers to the inner layers.

And the C++ syntax for that is that all superlcass stuff has to be done following
the ':' after a constructor signature definition. Sorry, blame Bjarne Stroustrup
for that ;-)

kind regards,

Jos
May 20 '07 #5
This should work

derived::derived(char* temp, int number)
{
base::base(temp);
value =number;
}
May 21 '07 #6
AdrianH
1,251 Expert 1GB
This should work

derived::derived(char* temp, int number)
{
base::base(temp);
value =number;
}
If it does, the the world will implode. Good thing it doesn't, eh? :D


Adrian
May 21 '07 #7
Please try this code

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class base
  7. {
  8. public:
  9.          base();
  10.          base( char*);
  11.  
  12.          char* return_string() { return string; }
  13. private:
  14.          char string[10];
  15. };
  16.  
  17. class derived:public base
  18. {
  19. public:
  20.             derived();
  21.             derived(char* , int);
  22.  
  23.             int returnValue( ) { return value; }     
  24.  
  25. private:
  26.          int value;
  27. };
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. base::base()
  35. {
  36. memset(string, 0, 10);
  37. }
  38.  
  39. base::base(char* temp)
  40. {
  41.      strcpy(string, temp);
  42.  
  43.      cout <<"\n"<< temp;
  44. }
  45.  
  46. derived::derived()
  47. {
  48. }
  49.  
  50. // my question is here...this works
  51. //derived::derived(char* temp, int number):base(temp)
  52. //{
  53. //    value =number;
  54. //}
  55.  
  56.  
  57. derived::derived(char* temp, int number)
  58. {
  59.     base::base(temp);
  60.     value =number;
  61. }
  62.  
  63.  
  64. void main()
  65. {
  66.     derived d("hello",45);
  67. }
May 21 '07 #8
AdrianH
1,251 Expert 1GB
What compiler are you using? You cannot call a constructor's body in this way. The line base::base(temp); is interpreted as declaring a var called temp with a type called base::base (base type located in the scope of base). Read here for more info.

For an example of this not working, try modifying your code to state what constructor is being called, by couting out at both base constructors. If your compiler is accepting it, it is because it is not telling you about var temp being shadowed. Under g++ this is considered an error.

BTW, main() should always be decared as returning an int.


Adrian
May 21 '07 #9
What compiler are you using? You cannot call a constructor's body in this way. The line base::base(temp); is interpreted as declaring a var called temp with a type called base::base (base type located in the scope of base). Read here for more info.

For an example of this not working, try modifying your code to state what constructor is being called, by couting out at both base constructors. If your compiler is accepting it, it is because it is not telling you about var temp being shadowed. Under g++ this is considered an error.

BTW, main() should always be decared as returning an int.


Adrian
Thank you for the link. The case which you are refering is quite different from what we are discussing.
I've tried this on VC++ 6.0 and it works absolutely fine.

Could you please justify your statement
"You cannot call a constructor's body in this way. " ??
Please post the Error which you got while compling this code on g++.

Even this is going to work
void main()
{
base::base("hellothere");
}

Here a temporary object will be created.
Also in our case. base::base() will NOT be intepreted as variable declaration.
The case you are talking about is this ( from your refrence link)

Foo x(Bar());
What main should return is secondary.
May 21 '07 #10
AdrianH
1,251 Expert 1GB
Thank you for the link. The case which you are refering is quite different from what we are discussing.
I've tried this on VC++ 6.0 and it works absolutely fine.

Could you please justify your statement
"You cannot call a constructor's body in this way. " ??
Please post the Error which you got while compling this code on g++.

Even this is going to work
void main()
{
base::base("hellothere");
}

Here a temporary object will be created.
Also in our case. base::base() will NOT be intepreted as variable declaration.
The case you are talking about is this ( from your refrence link)

Foo x(Bar());
What main should return is secondary.
OMG, if it works with VC++ 6.0, then it must be right. LOL ;)

I did try it and low and behold it did work without warnings (no surprise as it is a MS compiler ;)). But even it doesn't do what you think. Try and putting a line like these:
Expand|Select|Wrap|Line Numbers
  1. // Place in base::base()
  2. cout << "base::base() called.  this = " << this << endl;
  3.  
  4. // Place in base::base(char *) 
  5. cout << "base::base(char *) called.  this = " << this << endl;
  6.  
Oh, you might also want to put something similar in base's destructor.

It will surprise you by showing you that it is creating a temporary variable. Not calling the constructor on the current instance like it would if you put it where we stated it should be. (BTW, this is not what the standard says it should do which translate roughly into "If it looks like a declaration, it is.")

Under g++ (also not always the best compiler to use for such tests, but IMHO better than VC++) it does get it right in that it thinks that base::base(temp); is a declaration. Though even I was surprised that it would since the type appears to be the constructor, but apparently, base(temp) is equivalent to base::base(temp).

If you want the best test, Comeau is reportedly the best (comp.lang.c++ refer to it quite a bit) in implementing C++ in accordance to the standard. I've not bothered yet to download their compiler, but I will when I get my flaky drive repaired.

The link stating Foo x(Bar()) is only the tip of the iceberg. Foo(x); is equivalent to Foo x; under properly conforming compilers.

Have I justified myself enough?


Adrian
May 22 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: andrea_gavana | last post by:
Hello NG, I am trying to port a useful class from wxWidgets (C++) to a pure Python/wxPython implementation. In the C++ source code, a unique class is initialized with 2 different methods (???)....
1
by: R.Balaji | last post by:
Hi, How do I call the overloaded construction when I create the instance using the Reflection? for eg) namespace MySpace { Interface IOrganization
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
3
by: Mike Labosh | last post by:
Below, I want the first constructor to pass a default to the second constructor, but I can't seem to work out the syntax to do so. For example, in VB.NET I can do this: Sub New()...
4
by: Roger Webb | last post by:
Hey All, I looked through the news group and found a few threads on inheriting constructors...which I dont think is what I want to do here... since I'm dealing with issues between constructors...
3
by: Vera | last post by:
I built a class in VB.NET that has an overloaded constructor. It can either accept nothing, a string or an object Public Sub New( MyBase.New( End Su Public Sub New(ByVal strName As String...
7
by: cmay | last post by:
Can someone quick shed some light on this issue I am having? I wanted to create 2 constructors for my object, one that takes an ID value and one that takes a datarow. If ID value is provided,...
13
by: =?Utf-8?B?QW5kcmVhcw==?= | last post by:
Hi, I would like to get some thoughts on Overloaded constructors vs. Object initializations. Assuming that the class supports a default constructor, is there any reason to include overloaded...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.