Thread: A question about constructors...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    A question about constructors...

    Hi everyone. I've been learning C as my hobby for about 2 years now, and I've just recently decided that I've consumed enough knowledge to start working with C++ . Anyhow the question at hand is regarding constructors and inheritance.

    Assume that I had:

    Code:
    class Player
    .
    .
    .
    
    class Elemental : public Player
    .
    .
    .
    Now the problem that I'm having understanding is, when an Elemental object is created it also calls the Player class's constructor. For instance, maybe I don't want Player's constructor called every single time I create an Elemental object. Is there any way to get around this? I'm sure that there has to be a better way than how I'm doing this, because I'm a fledgling c++ programmer, lol.

    Here's a little snippet of code so that you can see what I'm talking about.

    Code:
        const int MAXP = 4; // Max count in Player array.
        const int MAXE = 15; // Max count in Elem array.
        
        Player pParty[MAXP] = { // Create array of characters.  With different values.
                 // Name   Status  Shield  Health  Mana
            Player("Paul", IsAlive,  2000,  150,    -1),
            Player("Joe"),
            Player("Bob"),
            Player("Jason"),
        };        
        
        Elem pElem[MAXE] = {
            Elem("Fire"),
            Elem("Blaze"),
            Elem("Flare"),
            Elem("Burn",BURNING),
            Elem("Ice"),
            Elem("Blizzard"),
            Elem("FlashFreeze"),
            Elem("Freeze",FROZEN),
            Elem("Wind"),
            Elem("MicroBurst"),
            Elem("Tornado"),
            Elem("Confuse",CONFUSED),
            Elem("Water"),
            Elem("Geyser"),
            Elem("Flood")
        };
    The output I get from the constructors on the console looks like this:

    Code:
    Constructing Player object Paul
            Negative found (-1) -- correcting.
    Constructing Player object Joe
    Constructing Player object Bob
    Constructing Player object Jason
    Constructing Player object Default
    Constructing Elem object Fire
    Constructing Player object Default
    Constructing Elem object Blaze
    Constructing Player object Default
    Constructing Elem object Flare
    Constructing Player object Default
    Constructing Elem object Burn
    Constructing Player object Default
    Constructing Elem object Ice
    Constructing Player object Default
    Constructing Elem object Blizzard
    Constructing Player object Default
    Constructing Elem object FlashFreeze
    Constructing Player object Default
    Constructing Elem object Freeze
    Constructing Player object Default
    Constructing Elem object Wind
    Constructing Player object Default
    Constructing Elem object MicroBurst
    Constructing Player object Default
    Constructing Elem object Tornado
    Constructing Player object Default
    Constructing Elem object Confuse
    Constructing Player object Default
    Constructing Elem object Water
    Constructing Player object Default
    Constructing Elem object Geyser
    Constructing Player object Default
    Constructing Elem object Flood
    Constructing Player object Default
    "Default" is a default name given to the object if a string wasn't specified as an argument when the object got created.

    Any ideas on how to get around this?
    Last edited by Wolve; 05-03-2005 at 06:03 AM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    before you decide to use inheritance make sure you have an "is-a" relationship.

    Is an elemental a player?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    6
    Heh, no. Elemental is for "Elemental Magic", which handles status effects that could eventually be given to the "Player" upon some kind of request. I was hoping to be able to store each form of magic in an array, but be associated with "Player" so that its effects could be passed down to variables inside the "Player" class.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Then dont inherit Elemental from Player. Instead pass a reference to a Player object into whatever member functions of Elemental that need it.
    i.e.
    Code:
    class Elemental
    {
       public:
            DoVisFXOnPlayer(Player& thePlayer)
            {
               // do your stuff on player here
             }
       private:
            .....
    };
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    6


    I just read a section on referencing objects in a poorly written book, "A Beginner's guide to C++". It's not very easy to understand when the authors spend about 5 minutes throwing together a chapter, hardly explaining something's various uses... I never thought to apply it to this. Thanks alot, man.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There are several reasons we use references like this. If at all poss it should be const and only nonconst if absolutely necessary.

    Why not pass by value?
    To pass by reference theres little to no overhead whereas to pass by value we would have to copy our object to the stack. It gets worse if we had inherited our passed class from another class and the function took the base class as a parameter then the object passed would become sliced and forever be a base object instead of a derived. So to combat these problems we always pass by reference. This also opens up the door to polymorphism where we make functions that take a base class reference and then give them derived objects and the right thing is done, but lets not walk before you can run.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I just read a section on referencing objects in a poorly written book, "A Beginner's guide to C++".
    Why not get a good book?

    I've just recently decided that I've consumed enough knowledge to start working with C++
    I don't know any C, and I learned C++. I've heard many people say that learning C first is not a good approach. It sounds like you should have jumped in quicker.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    6
    Stoner, thanks for pointing that out. I think you described it better than 10 pages in my retarded book just did ... I'll have to keep that in mind while I'm learning this stuff.

    Quote Originally Posted by 7stud
    Why not get a good book?

    I don't know any C, and I learned C++. I've heard many people say that learning C first is not a good approach. It sounds like you should have jumped in quicker.
    I don't think that's the case. C++ is just a superset of C, so in turn knowing C makes it easier to understand the concepts presented in C++. It's not really a question of if I can program in this language, it's just that I'm still in the early stages of learning it.

    Also, a very very very good book is "C Primer Plus", written by Steven Pratta. I considered getting a copy of "C++ Primer Plus", but this stupid book I picked up was in front of me at the bookstore, so I impulsively bought it lol.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't think that's the case. C++ is just a superset of C, so in turn knowing C makes it easier to understand the concepts presented in C++.
    C++ is mostly a superset of C, but not entirely, and there are concepts that are more suitable for C++ (OOP?).
    There are pros and cons, I suppose.

    Also, a very very very good book is "C Primer Plus", written by Steven Pratta. I considered getting a copy of "C++ Primer Plus", but this stupid book I picked up was in front of me at the bookstore, so I impulsively bought it lol.
    You could try Accelerated C++: Practical Programming by Example.
    I didnt start with that book, but it does look good for introducing C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Re: Passing by reference, aside from placing the "&" for the address of the object in your function declaration/definition, is there any difference in use? It seems like everything works identically (except, like you said, not having to produce a copy), but I just wanted to make sure before I get in too deep.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 03:01 PM
  3. question about constructors and exceptions
    By Elkvis in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2008, 06:15 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Replies: 2
    Last Post: 12-17-2001, 06:40 PM