Thread: A question about constructors...

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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