Skip to main content
GameDev.net gamedev.net
🔒 Locked

VS2003: compiler bug?

Started by Xtreme Apr 13, 2005 at 10:57 PM 1 replies 500+ views
Original Post
Xtreme
Xtreme
hello, I am wondering whether this is a microsoft c++ compiler bug. I have written the following code:

#include <iostream>

using namespace std;

class Parent
{
protected:
	int basedata;

public:
	Parent()
	{
		basedata = 1;
		cout << "Parent = " << this << endl;
	}

	void Show()
	{
		cout << basedata << endl;
	}
};

class Child1 :  public Parent
{
public:
	Child1()
	{
		basedata = 2;
		cout << "Child1 = " << this << endl;
	}

	void Show()
	{
		cout << basedata << endl;
	}
};

class Child2 :  public Parent
{
public:
	Child2()
	{
		basedata = 3;
		cout << "Child2 = " << this << endl;
	}

	void Show()
	{
		cout << basedata << endl;
	}

	//int basedata;
};


class GrandChild : public Child1, public Child2
{
public:

	GrandChild()
	{
		Child1::basedata = 2;
		Child2::basedata = 3;
		Parent::basedata = 4;
		//basedata = 4;

		cout << "GrandChild = " << this << endl;
	}
};

And the following main():

int main()
{
	Child1 Ch1;
	Child2 Ch2;

	Ch1.Show();
	Ch2.Show();

	GrandChild G;
	//G.ShowAdress();

	G.Child1::Show();
	G.Child2::Show();

	G.Child1::Parent::Show();
	G.Child2::Parent::Show();

	return 0;
}

Now, if you look carefully, we have a GrandChild class which is inherited from both Child1 and Child2 classes. If you look at GrandChild class, basedata on its own (which is commented in this code) will not compile since it is ambigious (ie, compiler doesnt know which path to take for basedata). However, if I do Parent::basedata just like in the above code, it is still ambigious but the compiler does not complain!! When you run the code, you will notice Parent::basedata is taken from the Child1 path!! If you rearrange the inheritence order to Child2 then Child1, then Parent::basedata is taken from the Child2 path. Clearly, the compiler should output an error for the above code (specifically for the Parent::basedata variable in GrandChild class) instead of compiling successfully. Any thoughts?
Yes! There are kangaroos in Australia but I haven't seen them...yet
mumpo
mumpo
Looks to me like it likely is a minor compiler bug (if that's what you call it when a warning/error is missing yet the code still compiles to a working executable). My guess would be that the case you show was simply overlooked by Microsoft because it is so rare: usually Child1 and Child2 would both have been derived from Parent with the 'virtual' keyword thrown in, causing there to be no difference between Child1::basedata and Child2::basedata.
MaulingMonkey
MaulingMonkey
Quote:
Original post by mumpo
Looks to me like it likely is a minor compiler bug (if that's what you call it when a warning/error is missing yet the code still compiles to a working executable).


Agreed - it SHOULD return an error because selecting one in the face of ambiguity will cause the worst kind of error - the kind of bastard that's a bastard to track down.

Topic Locked

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

Sign in to reply to this topic.