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

This is a discussion on nitializing parent Class's member variables from base's --- Base's Constructor within the C++ Programming forums, part of the General Programming Boards category; say for an example C extends B extends A e.g. A is Grand, P is Parent, and C is Child ...

  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,328
    __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,328
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    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, 11:03 AM
  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, 05: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, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21