[try Beta version]
Not logged in

 
 
Object with argument as a class member

Sep 20, 2013 at 11:27am
Write your question here.
I want to do something as below but the compiler is not allowing me to do.
There are three classes

1
2
3
4
5
6
7
8
9
10
11
Class A
{
};
Class B
{
};
Class C
{
    A a;
    B b(a);   
};


Please let me know the correct way of doing this
Sep 20, 2013 at 12:11pm
C++ is case sensitive. You can't capitalize keywords. class will work, Class will not.
Sep 20, 2013 at 1:03pm
... the compiler is not allowing ...

When the compiler does not like something, it will spit out an error message or warning. You should try to learn to read those messages, because they can help pointing out what the problem is.
Sep 20, 2013 at 2:06pm
Thanks Guys for reply...
I am able to solve the problem..

Explanation:
1) Its "class", just gave above peice of code as example {My typo error :)}
2) Class B was having private destructor and a public expilicit contructur taking argumnet.
3) I tried below and it works fine

Class C
{
C(A& a)
:B(a)
{
}
A a;
B b;
};

Please get back to me if my explanation is not clear info.. :)
Sep 20, 2013 at 2:42pm
you need to do something linke this:
1
2
3
4
5
6
7
8
9
class A
{

};

class B
{
  B(const A& a) // you use const to make sure you don't change anything in a
};

That should work at what you want.

Sep 20, 2013 at 2:47pm
Thank you :) incarporated your review comment...
Topic archived. No new replies allowed.