Skip to main content
gamedev.net
🔒 Locked

weird inheritance problem

Started by Access Jan 6, 2005 at 6:16 PM 3 replies 400+ views
Original Post
Access
Access
In C++, if you make a derived class from a base class, when u make derived object, the constructor of the base class should be called right? Well it works for me but only halfway through,..i am using MSVC++ 6.0 standard edition and i tried this:


#include <iostream>	// Include our standard header
#include <windows.h>
#include <fstream>	// Include this to use file streams

using namespace std;								    // set our namespace to standard;

class Cpolygon
{
protected:
	int height;
	int width;
public:
	void SetArea(int a,int b){height=a; width=b;}
	Cpolygon(){cout<<"empty def const for base class(Cpolygon)"<<endl;}
	Cpolygon(int tri){cout<<"base class (cpolygon) const sent the value of "<<tri<<endl;}
};


class Ctriangle:public Cpolygon
{
public:
	Ctriangle(){}
	Ctriangle (int a,int b):Cpolygon(b){ cout<<"derived object constructor wit value of "<<a<<endl;}
	int Area(){return(0.5*height*width);}
};

  
int main()
{
	Ctriangle me(5,6);    //calls the base class's second constor fine
	Ctriangle me2();     //this should call base's def const but doesnt
	

	return 0;
}




Now on my compiler that builds and runs. the creation of Me, calls the base class second constructor. ( i see the message it prints if called) the creation of Me2, should call the default constructor of the base class but doesnt seem to do so. ( i dont see any message verifying that it does) Why does this happen? Is this some bug with MSVC 6.0 as one person of the IRC channel has suggested. BUT, if i change the first constructor of Ctriangle to accept a parameter. (as such: Ctriangle(int bla){} ) and create an object using that constructor as such: ( ctriangle me2(6); ) then the default constructor of the base class DOES get called. Its strange,...is the base class's default constructor only supposed to be called when the derived takes a parameter?
______Yes I think the rev pad sucks/so does 50 and the DS. I like action and am not ashamed. I prefer brunettes but hate chocolate. Yes,I have opinions!!! Rate as you will.
choffstein
choffstein
It is this line:
Ctriangle me2();     //this should call base's def const but doesnt

All you want is "Ctriangle me2;"

Essentially the compiler is taking it as a method declaration, instead of creating an instance of a class.

[Edited by - visage on January 6, 2005 6:21:20 PM]
Enigma
Enigma
There's a rule of thumb in C++ that pretty much anything that can be parsed as a function declaration will be parsed as a function declaration. Ctriangle me2(); does not define an instance of Ctriangle. It declares a function named me2 which takes no parameters and returns a Ctriangle. You want Ctriangle me2; (no parenthesis).

Enigma
Access
Access



Thanks a lot for your help. I cant believe my stupid mistake.

NOTE: this is advice for everyone. Its also probably best to confirm things on the forum first. I was going to write in my c++ learning book that it only works when the derived class is called with a parameter. If i didnt come here to get corrected who knows how long i would have carried such incorrect knowledge for in my head.
the thought actually scares me.

Anyways, thanks.
______Yes I think the rev pad sucks/so does 50 and the DS. I like action and am not ashamed. I prefer brunettes but hate chocolate. Yes,I have opinions!!! Rate as you will.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.