Results 1 to 4 of 4

Thread: C++ instructions storage (??)

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    C++ instructions storage (??)

    Sorry about the awkward subject line; I'm not really sure how to phrase this question.

    Basically, how does C++ store instructions (specifically for classes)? Are they stored with each instance of the class (like the variable data), or are they stored in only one place?

    I ask because I like OOP. I think it is (arguably) the best programming paradigm currently in existence because it is a natural extension of the human thought process. I know C++ has issues regarding speed and size, unfortunately.

    Normally, this wouldn't be an issue, but what if a class I need stores just a little bit of data but has a large number of functions associated with it? Would it be efficient to use a class, or would it be more efficient to use a more traditional C style struct and separate functions?

    For example, lets say we have a class that stores a 3-d vector:

    Code:
    class Vector {
    
    public:
       int x, y, z;  // This is the primary part of the 
                    // class that needs to be unique
                    // for each instance of this class.
       
       void normalize ();
       int getMagnitude ();
       int getAngleDegrees ();
       int getAngleRadians ();
       void crossProduct (Vector v);
       int getDotProduct (Vector v);
       void multScalar (int scalar);
       void divideScalar (int scalar);
       void add (Vector v);
       void sub (Vector v);
       // ect...
    
    };
    This class has a very high data to method ratio. All of this class' methods are identical between each instance of the class. They work on different data (obviously), but the instructions are essentially the same. If a copy of the instructions are stored for each instance, that would be a huge waste of space (especially if one needed to have hundreds or thousands of vectors) and I suppose it would be better to use the traditional C style. What do you think? Any ideas? Thanks.

  2. #2
    Guest

    Re:C++ instructions storage (??)

    One question, why do you not stick you're ints in private?

  3. #3

    Re:C++ instructions storage (??)

    No, there isn't seperate instanciations with instanciations of classes.

    What C++ does (or, rather, the compiler), is create one instance of each function for that class, and for each call of that function in the application, C++ calls the function as if it were a C function, except that it passes the object itself as the implicit first parameter (which you can address using the this pointer.)
    Code:
       Vector vec;
       vec.normalize();
       //Once it runs through the compiler, the call would act more like the following:
       normalize(vec);

  4. #4
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re:C++ instructions storage (??)

    [quote author=Lovechild link=board=9;threadid=4003;start=0#39922 date=1025597202]
    One question, why do you not stick you're ints in private?
    [/quote]

    Normally, I would try to keep all data encapsulated, but with this class I'd have to add at least six more methods

    Code:
    int getX ();
    void setX (int newX);
    //ect...
    If sans is correct, then I guess this wouldn't be a big issue.

Similar Threads

  1. Mac Music Storage Program
    By koala in forum Windows - General Topics
    Replies: 1
    Last Post: 09-18-2007, 12:28 PM
  2. Spyware removal instructions
    By genesis in forum Windows - General Topics
    Replies: 0
    Last Post: 01-26-2005, 07:47 PM
  3. Hotbar Removal Instructions and Help
    By regix in forum Windows - General Topics
    Replies: 0
    Last Post: 01-26-2005, 07:07 PM
  4. WildTangent Removal Instructions and Help
    By regix in forum Windows - General Topics
    Replies: 0
    Last Post: 01-26-2005, 07:05 PM
  5. Surferbar Removal Instructions
    By regix in forum Windows - General Topics
    Replies: 0
    Last Post: 01-26-2005, 07:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •