Global Scope have limitations :(
1 - can create and initialize a variable;
2 - we must change a variable static class member.
i mean create a member on class base, and change it's value using an instance on Global Scope
i mean create a member on class base, and change it's value using an instance on Global Scope
I don't quite know what you mean, perhaps give an example of the problem you're trying to solve?
But it sort of sounds like you're going for what's known as a Singleton pattern. The overuse of it can lead to some pretty unmaintainable code, especially when we get into multi-threading, but it can be nice to use sparingly.
class NumberManagerderived: public NumberManager
{
};
NumberManagerderived::NumberManager::InternalNumberManager NumberManager::number_manager;
- error because isn't a NumberManagerderived member or non static member;
- seen static can't be changed by instances.
i think... i think... that i'm locking for is the function Polymorphism.
like virtual function, but the derived class MUST have the function prototype.
what you can advice me more?
(maybe you think that i'm going to off topic... i'm sorry, i'm not... i'm trying to see if i can do it... my explain what i need isn't easy, but i'm trying... with other words i'm trying do like events: change their values on Global Scope without derived\instance class redeclaration)
class test
{
public:
virtualvoid hello();//i use virtual only for show you that it's for override it
};
test tsTest;
void tsTest::hello()
{
cout << "hello world";
}
test tsTest2;
void tsTest2::hello()
{
cout << "hello world2";
}
these code have some errors. but is what i need. the test instance must permit i define(if i need) the hello() on Global Scope section.
what you can advice me?
So you just want polymorphism? You can't define a function on a variable, but you can override the dynamic (virtual) base class functionality in derived classes.
#include <iostream>
using std::cout;
class test
{
public:
virtual ~test() {} // Also a virtual desructor is usually required
virtualvoid hello() = 0; // virtual is REQUIRED for polymorphism.
// Although the = 0 makes it a pure virtual, which isn't required.
};
class test1 : public test
{
public:
void hello() override
{
cout << "hello world\n";
}
};
class test2 : public test
{
public:
void hello() override
{
cout << "hello world2\n";
}
};
int main()
{
test1 my_test1;
test2 my_test2;
test* my_test = nullptr;
my_test = &my_test1;
my_test->hello();
my_test = &my_test2;
my_test->hello();
}