Anton
Anton

Reputation: 185

Static member declaration c++11

I create a basic IBasic interface with a static field

class IBasic
{
public:
    IBasic();
    virtual ~IBasic();

    static std::vector< std::vector<char> > Field;
};

from which the Inherit class is inherited:

class Inherit : public IBasic
{
public:
    Inherit(int);
    ~Inherit();
    void Foo();
};

The Inherit class makes some manipulations with Field static member in constructor/or member function. In order to create an instance of the Inherit class, we need to explicitly declare a static field in the main.cpp before the main function:

#include "Basic.h"
#include "Inherit.h"

std::vector< std::vector<char> > IBasic::Field;

int main()
{
    Inherit(10);

    return 0;
}

The questions are:

  1. In what namespace does the static method actually exists (global?)? Because I know that static field/function is not a class member in fact.
  2. Is there another way to declare this static method, for example, in a class file, inside a main function, or through creation unnamed namespace? Is it only one right variant?
  3. How is right? What should be considered first of all?

Upvotes: 0

Views: 397

Answers (3)

Francis Cugler
Francis Cugler

Reputation: 7925

Here's an example usage of a static member without any inheritance.

SomeClass.h

#ifndef SOME_CLASS_H
#define SOME_CLASS_H

class SomeClass {
private:    
    int x;
public:
     static SomeClass* const get(); // Needed For Using class to get this pointer
     SomeClass();

     int  getX() const { return x; }
     void setX( int val ) { x = val; }
};

#endif // SOME_CLASS_H

SomeClass.cpp

#include "SomeClass.h"

static SomeClass* s_pSomeClass = nullptr;

SomeClass::SomeClass() {
    s_pSomeClass = this;
}

SomeClass* const SomeClass::get() {
    if ( nullptr == s_pSomeClass ) {
        // throw exception
    }
    return s_pSomeClass;
}

Another class using above class as a static member

OtherClass.h

#ifndef OTHER_CLASS_H
#define OTHER_CLASS_H

class SomeClass; // Forward Declaration  

class OtherClass {
private:
    static SomeClass* pSomeClass;  // The Static Member to this class
    int y;

public:
    OtherClass();

    int  getY() const { return y; }
    void setY( int val ) { y = val; }

    void useSomeClassToSetY();
};

#endif // OTHER_CLASS_H

OtherClass.cpp

#include "OtherClass.h"
#include "SomeClass.h"

SomeClass*  OtherClass::pSomeClass = nullptr;

OtherClass::OtherClass() {
    if ( nullptr == pSomeClass ) {
        pSomeClass = SomeClass::get();
    }
}

void OtherClass::useSomeClassToSetY() {
    // First Set X To Some Value:
    pSomeClass->setX( 10 ); // Use of Static Member
    y = pSomeClass->getX(); // Use of Static Member
}

Static members still belong to the class, but they have static storage.

Upvotes: 1

n. m. could be an AI
n. m. could be an AI

Reputation: 120239

A static member of a class is a member of its class (that's a tautology) and its class namespace (a class is a namespace). It is not a nember of any other namespace.

A non-const static data member of a class must be defined exactly once in a program, outside of any class, in the same namespace its class is defined in (a global namespace in your case). A header file is inappropriate place for such declaration. It is normally placed in an implementation .cpp file that goes together with the header file.

Having said that, an interface should not have any static data members, much less public ones. It is most likely a grave design error.

Upvotes: 5

user0042
user0042

Reputation: 8018

  1. In what namespace does the static method actually exists (global?)? Because I know that static field/function is not a class member in fact.

It is declared in scope of the class. In fact the static variable is a class member, your assumption is wrong.

  1. Is there another way to declare this static method, for example, in a class file, inside a main function, or through creation unnamed namespace? Is it only one right variant?

The usual way is to define it in the translation unit that contains the function definitions for the class.

  1. How is right? What should be considered first of all?

There's no right or wrong way, but as mentioned definition in the same translation unit as the class function definitions is the usual way.

Upvotes: 4

Related Questions