-3

I have a c++ class which has static const and static variable on it. During startup I initialize all the static const variables with some string values and all the static variables with zeros.

Then I create 1st instance of that class and tried to modify the static variable alone. Then I create 2nd instance of the same class and tried to write some other value other than what I wrote for the 1st instance.

Later, when I tried to access the static variable of the 1st instance, it hold the 2nd instance values. while looking into the memory location I found variable pointer address are same for both instances.

Is it something I wrote wrongly?

My code snippet:

#include "stdafx.h"
#include <time.h>
#include <stdlib.h>

CBase::CBase(const myStruct *myStructPtr) {
    initStruct(myStructPtr);
}

CBase::~CBase() {}

void CBase::initStruct(const myStruct *myStructPtr) {
    m_myStructPtr = const_cast<myStruct *>(myStructPtr);
    if (m_myStructPtr) {
        uint8_t nNumDatas = (m_myStructPtr->numOfOutputs) ? (m_myStructPtr->numOfOutputs - 1) : 0;
        if (nNumDatas) {
            for (int idx = 0; idx < m_myStructPtr->numOfOutputs - 1; idx++) {
                if (m_myStructPtr->someNumber[idx])
                    m_myStructPtr->someNumber[idx] = (unsigned int)rand();
            }
        }
    }
}

void CBase::printSomeNumber() {

if (m_myStructPtr) {
    uint8_t nNumDatas = (m_myStructPtr->numOfOutputs) ? (m_myStructPtr->numOfOutputs - 1) : 0;
    if (nNumDatas) {
        for (int idx = 0; idx < m_myStructPtr->numOfOutputs - 1; idx++) {
            printf("\n printing : name %s, print number : 0x%x", m_myStructPtr->someName[idx],m_myStructPtr->someNumber[idx]);
        }
    }
}
}

const char * CDerived::scm_acsomeName[] = { "INIT", "REQ", NULL };
unsigned int CDerived::sm_aunsomeNumber[] = { 0, 0xDEADBEEF };

const myStruct CDerived::myDerivedStruct = {
    3, scm_acsomeName, sm_aunsomeNumber
};

int main()
{
    CBase *FirstDerived = new CDerived();
    CBase *SecondDerived = new CDerived();

FirstDerived->printSomeNumber();
SecondDerived->printSomeNumber();
while (1);
return 0;
}

My header file:

#include <stdint.h>
#include <string>
#include <iostream>

struct myStruct {
    uint8_t numOfOutputs;
    const char **someName;
    unsigned int *someNumber;
};

class CBase
{
public:
    CBase(const myStruct *myStructPtr);
    virtual ~CBase();
    void printSomeNumber();
private:
    CBase(const CBase&);
    void initStruct(const myStruct *myStructPtr);

protected:
    const myStruct* m_myStructPtr;
};

class CDerived : public CBase {
private:
    static const myStruct myDerivedStruct;
    static const char *scm_acsomeName[];
    static unsigned int sm_aunsomeNumber[];

public:
    CDerived() : CBase(&myDerivedStruct) {}
    virtual ~CDerived() {}
};

console print message:

printing : name INIT, print number : 0x0
 printing : name REQ, print number : 0x4823
 printing : name INIT, print number : 0x0
 printing : name REQ, print number : 0x4823
kar
  • 495
  • 1
  • 8
  • 19

1 Answers1

1

Is it something I wrote wrongly?

No, that is exactly how static data members work. You get one instance per class.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Ok I understood that I cannot use static for this kind of functionality. But how to implement such a feather which could be a part of the same structure? any workaround?? – kar May 07 '16 at 00:44
  • @kar I have no idea what kind of functionality you're trying to implement. I just answered the question you seemed to be asking. – juanchopanza May 07 '16 at 07:19