CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: structures

  1. #1
    Join Date
    Mar 2004
    Posts
    32

    structures

    i am haveing 3 structures

    1 struct1

    2 struct2

    3 struct 3

    i want to send any on of them as an arguments for a function

    how can i declare the function prototype


    i want to send any 1 structure as arguments
    Last edited by ravirams; April 22nd, 2005 at 10:33 AM. Reason: some addition

  2. #2
    Join Date
    Dec 2004
    Location
    Brasov
    Posts
    267

    Re: structures

    Do you want to use a single function which to work with all of this 3 structures?

  3. #3
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: structures

    Quote Originally Posted by ravirams
    i want to send any on of them as an arguments for a function
    As I presume that there is something common in the nature of the structures, I would recommend using classes and inheritance.

    Store common attributes in the base class, and send the base class as a parameter.
    Code:
      class CBase
      {
      public:
      void Dance () {};
      ...
      };
    and...
    Code:
      class CClass1:public CBase
      {
      ...
      };
      class CClass2:public CBase
      {
      ...
      };
      class CClass3:public CBase
      {
      ...
      };
    Have a Function:
    Code:
      void MyFunc (CBase * pBase)
      {
      pBase->Dance ();
      ...
      }
    Use it this way:
    Code:
    void CreateObjects ()
      {
      	 CClass1 * pC1 = new CClass1 ();
      	 CClass1 * pC2 = new CClass1 ();
      
      	 // Same function, two dissimilar objects
      	 MyFunc (pC1);
      	 MyFunc (pC2);
      
      	 delete pC1;
      	 delete pC2;
      }
    Note: I have not compiled the above.
    A better sample that explains Inheritance and Polymorphism is here.
    Last edited by Siddhartha; April 22nd, 2005 at 11:00 AM.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: structures

    Quote Originally Posted by ravirams
    i am haveing 3 structures

    1 struct1

    2 struct2

    3 struct 3

    i want to send any on of them as an arguments for a function

    how can i declare the function prototype

    i want to send any 1 structure as arguments
    You can make them inherit from a base structure so you can treat them polymorphically, in which case the function prototype will use the base structure, or provide an overloaded method each taking a different structure.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Wink Re: structures

    If Virtual Functionalities and Polymorphism have to be used, using a class is best recommended.
    Reason:
    Quote Originally Posted by MSDN
    In C++, a structure is the same as a class except that its members are public by default.
    Perhaps, it's a good time to move to classes.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: structures

    Quote Originally Posted by Siddhartha
    Perhaps, it's a good time to move to classes.
    As that note says, the only difference between structs and classes in C++ are accessors, but I'm not advocating the usage of structs over classes.

    PS: I've already taken part in a long debate in the C# forum about structs and classes in C# I'm not going to do it again, so soon...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Talking Re: structures

    Quote Originally Posted by cilu
    I'm not advocating the usage of structs over classes.
    I did not quote you - I was addressing the OP - 'coz he uses struct.

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: structures

    Quote Originally Posted by Siddhartha
    If Virtual Functionalities and Polymorphism have to be used, using a class is best recommended.
    Why would that be?

    In C++ there are only two differences between a class and a structure:
    • By default, the members of a class are private while the members of a structure are public
    • By default, a struct inherits publicly while a class inherits privately

    In other words...everything that a class can do, a structure can as well...

  9. #9
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Talking Re: structures

    Quote Originally Posted by Andreas Masur
    Why would that be?

    In C++ there are only two differences between a class and a structure:


    • By default, the members of a class are private while the members of a structure are public
    • By default, a struct inherits publicly while a class inherits privately
    In other words...everything that a class can do, a structure can as well...
    The reason is exactly what you mentioned - in C++ (today) a struct is given all the functionalities of a class - baring defaults.

    Given this situation, perhaps, it is best to call a struct being used as a class, a class.
    Last edited by Siddhartha; April 23rd, 2005 at 03:57 PM.

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: structures

    Quote Originally Posted by Siddhartha
    The reason is exactly what you mentioned - in C++ (today) a struct is given all the functionalities of a class - baring defaults.

    Given this situation, perhaps, it is best to call a struct being used as a class, a class.
    Ahh...okay...I thought I missed some technical reason...however, I still prefer structs over classes if I simply need to hold data...sometimes it is not worth adding the overhead of accessors....granted they will be inlined anyway...

  11. #11
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: structures

    Quote Originally Posted by Andreas Masur
    Ahh...okay...I thought I missed some technical reason...however, I still prefer structs over classes if I simply need to hold data...
    Me too, if holding data is all that it needs to do.
    Quote Originally Posted by Andreas Masur
    sometimes it is not worth adding the overhead of accessors....granted they will be inlined anyway...
    I agree with you there - are accessor methods inlined as a result of optimization (like the rest of code), or does the compiler intelligently detect them as accessors-methods, and inline them intentionally?
    Not sure if I am clear in my query...

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: structures

    They are inlined if you declare them as inline (though you cannot be sure that a function declared as inline will be actually inlined by the compiler), or if you implement them in the class declaration:
    Code:
    class foo
    {
       int data;
    public:
       // this is an inline function
       int GetData() const
       {
         return data;
       }
    };
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #13
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Red face Re: structures

    Guys, sorry for my ignorance, but i think accessor functions re those which re used to access the private data members right (pls correct me if iam wrong)?

    Ciao
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  14. #14
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: structures

    Quote Originally Posted by logan
    Guys, sorry for my ignorance, but i think accessor functions re those which re used to access the private data members right (pls correct me if iam wrong)?
    Yes, you are right.

    Presumably: if they are small enough, they become inline.

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