what type of variable we can change outsited the clas?

besides static, what variables we can change outside the class?
(on Global Scope section)
Static members are not necessarily accessible outside the class.
1
2
3
4
5
6
7
class A{
    static int foo;
};

void bar(){
    A::foo = 0; //error
}
Global variables.
Local variables.
Anything pointed to or referenced by the class members, or the global or local variables.

And if you don't mind creating pointers yourself, you can read memory that the OS allows you to.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Example program
#include <iostream>

class NumberManager {   
  public:   
    static int next_number()
    {
        return number_manager.number++;
    }

  private: 
    struct InternalNumberManager {
        int number;
        
        InternalNumberManager()
        : number(1) { }
    };
    
    static InternalNumberManager number_manager;

    // make constructor private
    NumberManager() {}
};
// C++ needs to define static members externally. I'm not sure why.
NumberManager::InternalNumberManager NumberManager::number_manager;


int foo()
{
    return NumberManager::next_number();   
}

int bar()
{
    return NumberManager::next_number();   
}

int main()
{
    std::cout << foo() << std::endl;
    std::cout << bar() << std::endl;
}


output:
1
2


Further reading:
https://stackoverflow.com/questions/5803953/what-is-a-static-constructor
https://stackoverflow.com/questions/5301666/what-is-the-rationale-for-not-having-static-constructor-in-c
Last edited on
now see, for what i really need:
1
2
3
4
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)
Sorry, but I have no idea what it is you're asking. It's like you're asking three different questions simultaneously.

Can you explain in the simplest possible terms what problem you're trying to solve?
see these sample with some errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class test
{
public:
       virtual void 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?
It's not possible to do that with virtual functions. Here's one possible implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class test{
    std::function<void(test &)> hello_impl;
public:
    test(std::function<void(test &)> &&cb): hello_impl(std::move(cb)){
        if (!this->hello_impl)
            throw std::runtime_error("Parameter must be a valid function!");
    }
    void hello(){
        this->hello_impl(*this);
    }
}

void hello1(test &This){
    cout << "hello world";
}

void hello2(test &This){
    cout << "hello world2";
}

void hello3(){
    cout << "hello world3";
}

test tsTest(hello1);
test tsTest2(hello2);
test tsTest3([](test &){ hello3(); });
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
using std::cout;

class test
{
public:
    virtual ~test() {} // Also a virtual desructor is usually required
       virtual void 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();
  
}


There's also CRTP
https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

Edit: helios' example using std::function probably matches what you want closer.
Last edited on
ganado: try, on test2, define the hello() ouside without a prototype function?
ganado: try, on test2, define the hello() ouside without a prototype function?
That's not possible.
i must try more for try see how i can do it ;)
thanks to all for all.. thank you
Topic archived. No new replies allowed.