|
|
How can I instantiate a class inside another class
Hello,
Here is my Code
class A
{
int x;
public:
A()
{
x=10;
}
A(int x_):x(x_){}
};
class B
{
A a;
B(){}
};
Now I want to call the second constructor of A (other than default
constructor) from B. IF there any possible way other than using
pointer variable?
Josekutty K K
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kkjoseph
|
1/15/2005 2:52:56 AM |
|
You can initialize in the member initializer.
class B
{
A a;
B() : a(10) {}
};
--lsu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
L
|
1/15/2005 10:30:47 AM
|
|
kkjoseph@gmail.com (Josekutty K K) writes:
> class A
> {
> int x;
> public:
> A()
> {
> x=10;
> }
> A(int x_):x(x_){}
> };
>
> class B
> {
> A a;
> B(){}
> };
>
> Now I want to call the second constructor of A (other than default
> constructor) from B. IF there any possible way other than using
> pointer variable?
I am not sure what are asking for exactly, but
class B
{
A a;
B() : a(3) {}
};
uses the second A constructor to initialize a, and there is no
pointer variable involved.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Thomas
|
1/16/2005 4:22:28 AM
|
|
Josekutty K K wrote:
> class A
> {
> int x;
> public:
> A()
> {
> x=10;
> }
> A(int x_):x(x_){}
> };
>
> class B
> {
> A a;
> B(){}
> };
>
> Now I want to call the second constructor of A (other than default
> constructor) from B. IF there any possible way other than using
> pointer variable?
If I understand you correctly and you want to invoke A::A(int x_) from
B's constructor, do it whilst initialising B as follows:
class B
{
A a;
B() :
a(42)//or whatever value you require
{
}
};
Rgds,
MikeB
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
MikeB
|
1/16/2005 4:23:12 AM
|
|
|
3 Replies
350 Views
(page loaded in 0.076 seconds)
Similiar Articles: How can I instantiate a class inside another class - comp.lang.c++ ...Hello, Here is my Code class A { int x; public: A() { x=10; } A(int x_):x(x_){} }; class B { A a; B(){} }; Now I want to ca... Solution to "cannot instantiate abstract class due to following ...Dear comp.lang.c++.moderated, Using MS Visual C++, I am trying to create two abstract classes, one is a member of another, after that I am trying to ... Making use of run-time dynamic linking (Examples?) - comp.lang.c++ ...... create a factory that can access and instantiate the correct derived class. ... derived classes, since another user could add a derived class ... instead of engine from inside ... Passing Variable from One class to another, using an ...How can I instantiate a class inside another class - comp.lang.c++ ... Passing Variable from One class to another, using an ... How can I instantiate a class inside ... nitializing a static vector <> of integers (this static vector... siginfo_t* info = 0, void* > context = 0) = 0; > }; > > Then I have another class ... int ... nitializing a static vector <> of integers (this static ... within a class ... forward declaration of 'struct::Class' - comp.lang.objective-c ...Either #include A.hh or define A inside of namespace mySpace in another file, or, if A is only used by B, as an inner class inside of the B class definition. Enum type inside a class (matlab <-> java interface) - comp.soft ...Web interfaces are sure hard to operate. Another ... decimal to Character - comp ... Enum type inside a class (matlab <-> java interface) - comp ... For example, javaMethod ... class definition containing member of own type - comp.lang.c++ ...Here you're instantiating std::vector, and must not supply an incomplete type to do ... most types of data members (including the type of signal_handlers_) inside the class ... getContentPane().add - comp.lang.java.guiHi, I want to get a window inside the GUI I have and there is a problem with ... private JFrame simulatieFrame = new JFrame(); > > where Simulatie() is another class ... JFrame with a JDesktopPane - comp.lang.java.guiI had the hunch that it was JDesktoppane was the one responsible for drawing the ... With the JInternalFrame class you can display a JFrame-like window within another ... How can I instantiate a class inside another class - comp.lang.c++ ...Hello, Here is my Code class A { int x; public: A() { x=10; } A(int x_):x(x_){} }; class B { A a; B(){} }; Now I want to ca... Can't instantiate a class inside another classHi. The code below generates the errors: Error 1 error C2065: 'AboutDialog' : undeclared identifier Error 2 error C2065: 'About' : undeclared identifier ... 7/23/2012 1:40:56 PM
|
|
|
|
|
|
|
|
|