CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2001
    Location
    INDIA
    Posts
    125

    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.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    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

  4. #4
    Join Date
    Dec 2001
    Location
    INDIA
    Posts
    125

    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.

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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
  •  





Click Here to Expand Forum to Full Width

Featured