Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. inhereted static member referencing

inhereted static member referencing

Scheduled Pinned Locked Moved C / C++ / MFC
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harco
    wrote on last edited by
    #1

    I have the following class: class BaseClass { static char* wp; void Dynamic(char* s) { this->wp = s; } } and a derivative: class Derived : public BaseClass { // static char* wp; // have been trying this, not sure if needed } Basically, what I want to accomplish is this: I want to reference the static member wp of the derived class in the inhereted function and reference the BaseClass member in the Baseclass function so.. BaseClass *bc = new BaseClass(); Derived *d = new Derived(); bc->Dynamic("something"); d->Dynamic("something else"); After this code, I want bc::wp != d::wp, but alas, bc::wp == d::wp == "something else". I would LIKE to have bc::wp == "something" and d::wp == "something else". Anybody have any clues as to how I might realise this? And yes I do realise I could make them non-static, but I am interested in a way of doing this... if at all possible.

    A 1 Reply Last reply
    0
    • H Harco

      I have the following class: class BaseClass { static char* wp; void Dynamic(char* s) { this->wp = s; } } and a derivative: class Derived : public BaseClass { // static char* wp; // have been trying this, not sure if needed } Basically, what I want to accomplish is this: I want to reference the static member wp of the derived class in the inhereted function and reference the BaseClass member in the Baseclass function so.. BaseClass *bc = new BaseClass(); Derived *d = new Derived(); bc->Dynamic("something"); d->Dynamic("something else"); After this code, I want bc::wp != d::wp, but alas, bc::wp == d::wp == "something else". I would LIKE to have bc::wp == "something" and d::wp == "something else". Anybody have any clues as to how I might realise this? And yes I do realise I could make them non-static, but I am interested in a way of doing this... if at all possible.

      A Offline
      A Offline
      antlers
      wrote on last edited by
      #2

      If you want the derived class to have its own, separate copy of the static member, you have to declare it in the derived class. Then, you also have to implement Dynamic() in the derived class so it refers to the derived class' static member and not the base class' static member.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups