0

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?
3
  • 1. In class name namespace. 2. You can declare static in any .cpp file. 3. I think better is declare static in Basic.cpp. Commented Oct 23, 2017 at 8:33
  • 1
    @Unick Answers go down there vvvvvvvvvvv Commented Oct 23, 2017 at 8:35
  • There is no static method in your code Commented Oct 23, 2017 at 8:38

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, it is not an interface firstly, just base class, and secondly I change it to protected. Thanks for sharing your experience
4
  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.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.