Thread: nitializing parent Class's member variables from base's --- Base's Constructor

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    nitializing parent Class's member variables from base's --- Base's Constructor

    say for an example
    C extends B extends A
    e.g. A is Grand, P is Parent, and C is Child
    and A have a member variable called __data which is protected.
    so would it be possible to initialize A's __data from C through C's Constructor
    like C::C():__data(0)

    My Compiler (g++) fires error saying Class C doesnt have a field named __data

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    __data should be initialized by A's constructor. B should call A's constructor appropriately to make sure that it is initialized correctly. C should call B's constructor correctly to make sure that it is initialized correctly.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    But as its a protected data member shouldn't it be able to access it though ??

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, but not in the initializer list... a member can only be in the initializer list once because that is where it's constructor is called. It will automatically be constructed by its class (A in this case) whether you type it in the list or not, so adding it to C's list would double-construct, which is not allowed.

    You can access it inside C's constructor, but that is usually not as good of an idea as accessing it through A's interface.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Ok Thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And avoid names starting with "__" like the plague, because they are reserved for compiler implementation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static member (map) / constructor initialisation problem
    By drrngrvy in forum C++ Programming
    Replies: 9
    Last Post: 12-28-2005, 12:03 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Objects and member variables on the heap
    By homeyg in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2004, 06:16 PM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Replies: 5
    Last Post: 11-19-2002, 07:49 PM