Classes or Structs faster for Lists

This is a discussion on Classes or Structs faster for Lists within the C++ Programming forums, part of the General Programming Boards category; Woohoo, maybe I'm not wrong. I completely agree with Rod....

  1. #16
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Woohoo, maybe I'm not wrong. I completely agree with Rod.

  2. #17
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Class defaults to private, constructors can be private/protected.

    Code:
    struct blah {
    private:
     int a;
    };
    
    class blah2 {
     blah2() { }  // legal, private constructor
    };
    
    int main() {
     blah inst;
    // blah2 inst2; // illegal.. can't call private constructorf
     return 0;
    }
    Private constructors are neccesary for the singleton design pattern.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #18
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    omg, i cant believe you people post before not testing it first:
    here is an EXAMPLE of a private struct:

    struct mystr{
    private:
    int ignorant;
    public:
    void set(int);
    int get(void);
    };

    THERE YA GO A PRIVATIZED STRUCT.. so simple its hurting.

    ROD PLEASE GO LEARN C++. And learn to read, i said structs CANNOT inherit.

    ALSO, CLASS DEFAULT TO PRIVATE, YES THEY DO, UNLESS YOU SPECIFY IT TO BE PUBLIC. ITS PRIVATE!!!!!
    geez people test ur code first.
    Last edited by Liam Battle; 04-02-2002 at 10:29 PM.
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  4. #19
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Calm down there buddy.

  5. #20
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    I believe that it is better to have constructors public, (I hope u just forgot to type the public keyword). that's the way i do it. Im pretty sure compilers don't like private constructors, especially parameterized ones.

    Code:
    class blah2 {
        public:  //Forgot this
            blah2() { }  // legal, private constructor
    };
    
    int main() {
        blah inst;
        return 0;
    }

    //*********************

    Code:
    struct strone{
        int a;
    };
    
    class clsone{
        int a;
    };
    Now put this bit of code into a simple program and try to access the integer variable inside each of them, and you'll soon find out the difference. You just won't be able to do it in a class.

    BTW use code tags when typing code

  6. #21
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I was just showing that it was legal to have private constructors. There are real uses for private constructors... ala that singleton design pattern.

    http://www.google.com/search?q=singleton+design+pattern

    I've never used structs with private members.. usually structs have some minimal amount of implementation (perhaps a couple convienent constructors)and serve as little more than data aggregates.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #22
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    On a unix platform, I can use the nm command to see the name space for data items and functions. If MS has a similair command, you will see that stuctures and classes are exactly the same after the code is compiled. This is because, C++ is backward compatible with C. When the compiler compiles a class, the resulting object code is the same.

    So, ANYTHING, you can do in C++, can be done in C.

  8. #23
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    public: //Forgot this
    blah2() { } // legal, private constructor
    Why is that private? Can a constructor defined in the private section of a class be called like public construcors?

    blah2 obj; //call constructor
    obj = obj(); //calls constructor again

  9. #24
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The keyword struct means different things in C++ than in C. It is used differently as well, although the general idea remains the same. I don't do much C programming, and I don't have my C reference here at work so I can't gaurantee the accuracy of the following, however, I should be pretty close:

    Code:
    //declaration of a struct type in C
    typedef struct cStruct
    {
      int data;
      char name[80];
      //no functions possible in here
    };
    
    //declaration of a C struct variable in a program
    int main()
    {
      struct cStruct myStruct;
      //etc.
    
    
    declaration of a struct type in C++
    struct cppStruct
    {
      int data;
      char name[80];
      void showMembers();
    };
    
    
    //declaration of C++ struct variable in a program
    int main()
    {
      cppStruct myStruct;
      //etc.

    in C++ the only difference between a struct and a class is that the members of a struct are public by default and the members of a class are private by default. Therefore you can have inheritiance of a struct in C++, just like you can with a class. It is not commonly done, because most programmers use structs in C++ similar to the way they are used in C out of convention/respect, but there is no inherent restriction in the language itself as far as I know. It should be easy enough to check.


    Code:
    /*
    program demonstrating inheritance using a struct, proving the members of a struct are public by default, and members of a class are private by default.
    */
    
    #include <iostream.h>
    
    struct Base
    {
      //all members default to public access
      int bData;
      Base();
    };
    
    struct BaseChild : public Base
    {
        //these are still public by default
        BaseChild();
        int showData();
        
        //bData will be inherited as public by default as well.
        
        //using private access for protection of data in a struct
        private:
            int bcData;
    };
    
    Base::Base() : bData(23)
    {}
    
    BaseChild::BaseChild()
    {
       bcData = 101;
    }
    
    int BaseChild::showData()
    {
       return bcData;
    }
    
    class MyClass
    {
       //using default private access on purpose
       int cData;
    
       public:
          int showData();
    };
    
    MyClass::MyClass() : cData(4)
    {}
    
    int MyClass::showData()
    {
       return cData;
    }
    
    int main()
    {
      BaseChild baseChild;
      MyClass myClass;
      
      cout << "bData in baseChild is inherited, public and = "
               << baseChild.bData << endl; 
    
      //this line should cause compiler error so comment it out once 
      //it gives compiler error
      cout << "bcData in baseChild using public access = " 
              << baseChild.bcData << endl;
      
      cout << "bcData in baseChild using public accessor function  = "
              << baseChild.showData() << endl;
      
      //this line should cause compiler error, too.  so comment it out.
      cout << "cData in myClass object is "  << myClass.cData << 
                     endl; 
      
      cout << "using public accessor function cData in myClass is " 
              <<  myClass.showData() << endl;
     
      return 0;
    }
    Will try to compile at home when I have compiler available. For now code not tested by fire.
    Last edited by elad; 04-05-2002 at 03:00 PM.

  10. #25
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yup, once I cleaned up the code in my program a little bit, the errors predicted were present, and the code ran as expected, clearly demonstrating you can inherit from structs, that struct members default to public, and class members default to private.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  4. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 02:21 PM
  5. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21