Poteto, if you were really redefining the function I would have expected A().hello() to print "this is b\n" which is not the case obviously, because you are not redefining the function.
Cambalinho, I think what you might be after is overriding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
struct A
{
public:
virtualvoid hello()
{
std::cout<<"this is a\n";
}
};
struct B : public A
{
void hello() override
{
std::cout<<"this is b\n";
}
};
This allows you to call hello() through a pointer (or reference) to A and it will automatically, at runtime, decide which function to call depending on the type of object that it points to.
1 2 3 4 5 6 7 8 9 10
A *ptr;
A objA;
B objB;
ptr = &objA;
ptr->hello(); // prints "this is a"
ptr = &objB;
ptr->hello(); // prints "this is b"
i'm sorry, only answer now.
but i was thinking these method:
1 2 3 4
void hello()
{
std::cout<<"Hello\n";
}
and after sometime: can i redefinition the function?
1 2 3 4
void hello()
{
std::cout<<"hello world\n";
}
the text it's only a sample for i show what i mean.
but, by C++ terminology\rules i can't do it.. even we think that the 2nd 'override' the 1st.
- if we prototype the function, but we call it without definition, we will get an error;
- if we definition the function, we can't redefinition it again
maybe that's why we have the function object ;)
thank you so much to all for all
the big problem, on C++ rules, is that i can't change variables on Global Scope.
i'm not changing the C++ terminology\rules... i'm learn more from what i can for do what i need.
on Global Scope we can:
1 - create and\or initialize objects;
2 - create functions\class's\structures\enum\others;
3 - we can't change the variables values.
from these thread i getted the information and the correction that i need.
thank you so much for all to all... thank you