Your code gets many more errors due to lack of proper #include files. Let's use an example that actually cuts through all of this noise:
Code:
class B1
{ };
class B2
{
public:
B2() : m_var(0) {}
int m_var;
};
class D : public B1, public B2
{
public:
D(int s);
};
D::D(int s) : B1(), m_var(s)
{ }
int main()
{
D d(0);
}
Here is the error reported by the Comeau compiler:
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 17: error: "m_var" is not a nonstatic data member or base
class of class "D"
D::D(int s) : B1(), m_var(s)
^
1 error detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
The issue is that m_var is a non-static member of B2, meaning that the B2 object has to be fully constructed before you can refer to m_var. The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 29th, 2012 at 12:09 PM.
... The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.
Alternatively, you can provide a constructor for B2 that takes a value to be set to m_var, and initialize B2 instead of that variable.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
Your code gets many more errors due to lack of proper #include files. Let's use an example that actually cuts through all of this noise:
Code:
class B1
{ };
class B2
{
public:
B2() : m_var(0) {}
int m_var;
};
class D : public B1, public B2
{
public:
D(int s);
};
D::D(int s) : B1(), m_var(s)
{ }
int main()
{
D d(0);
}
Here is the error reported by the Comeau compiler:
The issue is that m_var is a non-static member of B2, meaning that the B2 object has to be fully constructed before you can refer to m_var. The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.
Regards,
Paul McKenzie
If I change the definition of D constructor like the following, I still get the same error. I thought that before the D constructor is called, B2 has been full constructed. Why'd I still get the same error? Thanks.
Your code gets many more errors due to lack of proper #include files. Let's use an example that actually cuts through all of this noise:
Code:
class B1
{ };
class B2
{
public:
B2() : m_var(0) {}
int m_var;
};
class D : public B1, public B2
{
public:
D(int s);
};
D::D(int s) : B1(), m_var(s)
{ }
int main()
{
D d(0);
}
Here is the error reported by the Comeau compiler:
The issue is that m_var is a non-static member of B2, meaning that the B2 object has to be fully constructed before you can refer to m_var. The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.
Regards,
Paul McKenzie
If I change the definition of D constructor like the following, I still get the same error. I thought that before the D constructor is called, B2 has been full constructed. Why'd I still get the same error? Thanks.
Alternatively, you can provide a constructor for B2 that takes a value to be set to m_var, and initialize B2 instead of that variable.
Thanks for your reply. You meant like this?
Code:
class B1
{
};
class B2
{
public:
B2(string sVar) : m_var(sVar) {}
string& m_var;
};
class D : public B1, public B2
{
public:
D(string str);
};
D::D(string str) : B1() , B2(str)
{
}
int main()
{
D d("abc");
cout<<d.m_var<<endl;
return 0;
}
But this is not what I want. Basically I thought m_var is member data of B2 and D is derived from B2. So D should contains m_var too. However as Paul pointed out, when I refer to m_var, B2 is not fully constructed, which means m_var doesn't exist yet.
I agree with you that D does contain m_var. But since B2 object is not fully constructed when m_var is referred, there is a compiler error. So I wonder how to fix that error. Thanks.
I agree with you that D does contain m_var. But since B2 object is not fully constructed when m_var is referred, there is a compiler error. So I wonder how to fix that error. Thanks.
Why is it important in your design to access m_var at the point where you're trying to access it? Programming isn't just about trying to get something to compile -- what is the overall reason to access non-constructed member variables?
OK, so m_var isn't available. It is available within the body of the constructor of D or you construct a B() object using the string, and in the constructor of B set m_var to that value. What is the advantage of trying to use m_var as you're using it, even though it is illegal to do so? Why can't the two cases covered by myself and Vladimir not work for your design?
Trouble is that in the original code m_var is a reference to string.
I got it to compile without errors with g++ 4.6.3
but somehow that feels wrong
It compiles with Comeau C++ also. The issue I have with it is that the string is passed by value, making the parameter a temporary object. I don't know off-hand what will happen to the reference after the function returns and the temporary goes away.
The wrinkle in this example is that the original string "abc" is a string constant, and not a true std::string. It gets converted to a std::string on the way through, but that converted item disappears has gone when the cout is invoked.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 31st, 2012 at 03:00 PM.
Bookmarks