Re: Instantiation of Base Class.
AY wrote:
I'm quiet sure the member data ' testData ' can't be initialized by
Msg::TradeMsg class nor by Msg::OrderMsg class. But wanted to double
check if there is any way, we can instantiate Msg::testData
What instance of Msg will have the value of its testData member changed?
from
TradeMsg && OrderMsg, preserving the class Msg declaration. Sample
code follows:
// Lets preserve the declaration of class Msg as is..
class Msg{
public:
int testData;
public:
Msg();
class TradeMsg;
class OrderMsg;
~Msg();
};
I don't think that Msg::TradeMsg and Msg::OrderMsg are base classes,
rather nested classes.
class Msg::TradeMsg{
public:
TradeMsg(){
// Can we instantiate the Msg datamember like
this ?
testData = 5;
}
~TradeMsg(){
}
};
According to 9.7 of N2691=08-0201, the Working Draft, no, unless
testData is a static member of Msg.
We can instantiate, if class Msg is declared as base and let TradeMsg,
OrderMsg inherit from " Msg ".
I don't think I'm clear on what you mean by instantiate here. Do you
mean construct? Change the value of?
Can we confirm if inheritance is the
only mechanism to instantiate the data member of class " Msg ".
You can do something like this (untested):
class M {
int a_;
public:
void a(const int i) { a_ = i; }
class N;
};
class M::N {
public:
N(M &m, const int i) { m.a(i); }
};
int main() {
M m;
M::N n(m,5);
}
But it's not clear to me what it is you want to accomplish.
LR
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]