Inheriting static attributes

This is a discussion on Inheriting static attributes within the C++ Programming forums, part of the General Programming Boards category; I'm unsure of this (I apologize in advance if any of the terminology/syntax is off) -- Say class A is ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    Inheriting static attributes

    I'm unsure of this (I apologize in advance if any of the terminology/syntax is off) --

    Say class A is an abstract class (has some/but not all pure virtual operations).
    Class A also has public 'static int temp' attribute. (pretend temp is used by the not-pure-virtual A operations).

    Say class B and class C both inherit from A

    Do classes B, C each get their own copy of temp, or is temp shared by all subclasses of A?

    ie. if I did this.
    B::temp = 5;
    C::temp = 6;

    What does B::temp = ?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    In my test, the static member was shared between all the classes A, B, and C. In your case, B::temp should be 6 since it was set to 6 last.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Of course, it's bad practice to do it. For that matter, it's bad practice for A to even make it possible.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  4. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM

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