Thread: Super Function like in JAVA

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Super Function like in JAVA

    I have question about constructors,

    If i have

    Code:
    Class A {
      private:
         int x;
         int y;
    }
    
    Class B : public A {
      private:
         int x;
         int y;
         int z;
    }
    Is there a way when I code the constructors I can leave the parent constructor and just do this->y = y; and since Class B calls the parent class, x,y would just be reduntent. This is similar to the command SUPER in JAVA. Just wondering. Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not quite sure what you mean. Are x from A and x from B supposed to be the same thing? (Because they aren't.)

    The equivalent of this Java snippet:
    Code:
    class Base
    {
      public Base(int arg1) {
      }
    }
    
    class Derived extends Base
    {
      public Derived(int arg1, int arg2) {
        super(arg1);
      }
    }
    ... is this C++ code:
    Code:
    class Base
    {
    public:
      Base(int arg1) {
      }
    }
    
    class Derived : public Base
    {
    public:
      Derived(int arg1, int arg2)
        : Base(arg1)
      {
      }
    }
    If you want to have easier refactoring (changing the base class) you can add a typedef for the base class at the start and call it super:
    Code:
    class Base
    {
    public:
      Base(int arg1) {
      }
    }
    
    class Derived : public Base
    {
      typedef Base super;
    public:
      Derived(int arg1, int arg2)
        : super(arg1)
      {
      }
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    In general, what you've shown there should be avoided at all costs (in any language). Your shadowing variables... tsk, tsk

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Another thing, B can not get to A's variables without going through A's interface since the variables are private.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yes... Good old C++. Only your friends can see your privates... not your children.


    Yeah... couldn't help myself. Sorry.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM