|
-
September 5th, 2002, 04:51 AM
#1
structure as a member of class
Hi all,
How to use structures as a class member in C++
e.g
class A
{
public:
struct b
{
int x
};
void display()
{ cout<<"Hi"}
};
Rajendrappa HS.
Siemens Public Communication Networks Ltd.
Garden City, INDIA.
-
September 5th, 2002, 06:45 AM
#2
Having a structure [or class] embedded inside your class is fine;
you need to actually instantiate an object of that struct, though.
Think of a class [or struct] as a cookie cutter; you need to actually
create the cookie [class/struct] from that cookie cutter.
--Paul
-
September 5th, 2002, 03:04 PM
#3
Yes, you can nest a struct and/or class inside a class.
Code:
class X
{
public:
struct Y
{
...
};
class Z
{
...
};
private:
Y m_MyStruct;
Z m_nMyClass;
};
Kuphryn
-
September 5th, 2002, 11:53 PM
#4
structure as a member of class
Hi all
class base
{
public:
struct Y
{
int a;
};
struct Z
{
int b;
};
private:
Y m_MyStruct;
Z m_nMyClass;
};
HOw to define constructor for this class?
thanks
RAJU
Rajendrappa HS.
Siemens Public Communication Networks Ltd.
Garden City, INDIA.
-
September 6th, 2002, 12:30 AM
#5
Y and Z are distinct classes and can have constructors of their own, if they need them. Also base can have a constructor. There's nothing special about any of these constructors.
Note that "class A { public: };" and "struct A {};" are equivalent. A struct is a class.
Jeff
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|