can i redefine a function?

Apr 20, 2018 at 10:44pm
can i redefine a function?
Apr 20, 2018 at 11:22pm
No, it's not possible.
Apr 20, 2018 at 11:23pm
In general, no. Read about the one definition rule (ODR):
http://en.cppreference.com/w/cpp/language/definition
Apr 20, 2018 at 11:26pm
thank you so much for correct me
Apr 21, 2018 at 2:03am
This person is refering to this topic (most likely).
http://www.cplusplus.com/forum/general/235177/

He was struggling with the fact that he has to define a function for every class.

You can redefine a function if you are doing inheritance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct A
{
public:
  void hello()
  {
    std::cout<<"this is a\n";
  }
};

struct B : public A
{
  void hello()
  {
    std::cout<<"this is b\n";
  }
};
Apr 21, 2018 at 3:15am
Of course A::hello() and B::hello() are not the same function.
Apr 21, 2018 at 3:47am
Yes, and?
Apr 21, 2018 at 3:52am
poteto wrote:
You can redefine a function if you are doing inheritance. [...]

That's not correct. No functions are redefined in the above. Loosely using the term "re-define" in this thread is misleading.
Last edited on Apr 21, 2018 at 3:54am
Apr 21, 2018 at 4:04am
Overloading, redefining, you are being too critical with terminology. You are not adding anything to the thread.
Apr 21, 2018 at 8:33am
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:
  virtual void 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" 
Apr 21, 2018 at 11:34am
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
Apr 21, 2018 at 1:15pm
> and after sometime: can i redefinition the function?

If there is a need for that, simulate it by using an extra level of indirection. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <functional>

std::function< void() > my_function = [] { std::cout << "Hello\n" ; } ;

int main()
{
    my_function() ; // Hello

    my_function = [] { std::cout << "hello world\n" ; } ;

    my_function() ; // hello world
}

http://coliru.stacked-crooked.com/a/2b7df808bb7698e3
Apr 21, 2018 at 2:03pm
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
Last edited on Apr 21, 2018 at 2:05pm
Topic archived. No new replies allowed.