-
September 9th, 2014, 03:29 AM
#1
c++ 11 - can i override functions without re-declare them?
see these sample:
Code:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
virtual void ola()
{
cout <<"hi" << endl;
}
test()
{
//nothing;
}
};
class test1 : public test
{
public:
test1()
{
//nothing;
}
};
void test1::ola()
{
cout << "hello" << endl;
}
int main()
{
//cout << test::a << endl;
return 0;
}
like you see, i don't re-declare the 'ola' function in 'test1' class, only in 'test' class.
the compiler tell me the 'ola' isn't member of 'test1'. in 'test' i put it 'virtual', but forgeting that, how can i override it without re-declare it?
Last edited by Cambalinho; September 9th, 2014 at 03:31 AM.
-
September 9th, 2014, 04:00 AM
#2
Re: c++ 11 - can i override functions without re-declare them?
You cannot override ola without declaring it in test1.
-
September 9th, 2014, 07:18 AM
#3
Re: c++ 11 - can i override functions without re-declare them?
depending on what exactly you want to achieve...
you may be able to get what you want via function pointers/function objects and lambda's. But this may be limited depending on what kind of access you expect to get to the base class.
Code:
class base
{
public:
base()
{
ola = [&](){ hello=1; };
}
void callola()
{
ola();
}
protected:
int hello;
std::function<void()> ola;
};
class derived : public base
{
public:
derived()
{
ola = [&](){ hello = 2; };
}
};
int main()
{
derived d;
d.callola(); // sets d.hello to 2
}
you can make ola public
which would allow direct access to all (and change) the function on the fly
but when called outside of of memberfunction of d, you won't have any access to d at all except through normal capture of the specific instance.
also note that it's not the same as a virtual, the virtual is global, this is per-instance. It is possible to have multiple instaces of the same class pointing to different implementations of ola().
-
September 9th, 2014, 10:47 AM
#4
Re: c++ 11 - can i override functions without re-declare them?
Originally Posted by OReubens
depending on what exactly you want to achieve...
you may be able to get what you want via function pointers/function objects and lambda's. But this may be limited depending on what kind of access you expect to get to the base class.
Code:
class base
{
public:
base()
{
ola = [&](){ hello=1; };
}
void callola()
{
ola();
}
protected:
int hello;
std::function<void()> ola;
};
class derived : public base
{
public:
derived()
{
ola = [&](){ hello = 2; };
}
};
int main()
{
derived d;
d.callola(); // sets d.hello to 2
}
you can make ola public
which would allow direct access to all (and change) the function on the fly
but when called outside of of memberfunction of d, you won't have any access to d at all except through normal capture of the specific instance.
also note that it's not the same as a virtual, the virtual is global, this is per-instance. It is possible to have multiple instaces of the same class pointing to different implementations of ola().
now imagine that i want change the lambda value in Global Scope section(outside of functions\class's\others)?
-
September 10th, 2014, 08:27 AM
#5
Re: c++ 11 - can i override functions without re-declare them?
you can't.
or rather, not as a generic solution to apply to all instances. you can only do that on a per-instance base.
since the lambda isn't a memberfunction, it doesn't automatically have a 'this'. The 'this' needs to be part of the capture clause and since you wantr it to be defined outside of the context of the object, it won't work.
You can create a class member pointer and change the pointer on the fly, but all the actual implementations would need to be defined as part of being class members. again, not what you seem to be aiming at.
the closest you could get would be to take the example in #4, make the ola member static (and thus apply to all instances).
'this' access becomes pointless, so add a (const) class reference as parameter
change the callola() to pass the *this.
you'll only be able to access public members since the ola() isn't a class member anymore.
there's probably a couple cludges you could apply to circumvent that, but we're already well into abusing things as they are.
With a question like that, I'm very much inclined to think you have a design issue, and you should go back to rethink the actual design/approach of your class.
-
September 10th, 2014, 10:21 AM
#6
Re: c++ 11 - can i override functions without re-declare them?
Originally Posted by OReubens
With a question like that, I'm very much inclined to think you have a design issue, and you should go back to rethink the actual design/approach of your class.
If I remember correctly, Cambalinho actually asked this question before, or questions along similiar lines. Something about using C++ as an intermediate language, but I didn't understand why the compiler/interpreter couldn't simply output valid C++ code if that was the case.
-
September 10th, 2014, 11:14 AM
#7
Re: c++ 11 - can i override functions without re-declare them?
Originally Posted by OReubens
you can't.
or rather, not as a generic solution to apply to all instances. you can only do that on a per-instance base.
since the lambda isn't a memberfunction, it doesn't automatically have a 'this'. The 'this' needs to be part of the capture clause and since you wantr it to be defined outside of the context of the object, it won't work.
You can create a class member pointer and change the pointer on the fly, but all the actual implementations would need to be defined as part of being class members. again, not what you seem to be aiming at.
the closest you could get would be to take the example in #4, make the ola member static (and thus apply to all instances).
'this' access becomes pointless, so add a (const) class reference as parameter
change the callola() to pass the *this.
you'll only be able to access public members since the ola() isn't a class member anymore.
there's probably a couple cludges you could apply to circumvent that, but we're already well into abusing things as they are.
With a question like that, I'm very much inclined to think you have a design issue, and you should go back to rethink the actual design/approach of your class.
i'm trying do a sample:
Code:
#include <iostream>
#include <string>
#include <functional>
using namespace std;
typedef std::function<void(void)> OnSomethingHandler;
class test
{
public:
test()
{
//nothing;
}
static OnSomethingHandler ola;
};
//OnSomethingHandler test::ola=NULL;
class test2 final : test
{
public:
test2()
{
//nothing;
}
}test2;
OnSomethingHandler test2::ola=[this]()
{
cout << "hello";
};
int main()
{
return 0;
}
but i get several errors:
- "ISO C++ does not permit 'test:la' to be defined as 'test2:la' [-fpermissive]"
- "invalid use of 'this' at top level"
what i'm doing wrong with your idea?
-
September 10th, 2014, 12:08 PM
#8
Re: c++ 11 - can i override functions without re-declare them?
Originally Posted by Cambalinho
but i get several errors:
- "ISO C++ does not permit 'test: la' to be defined as 'test2: la' [-fpermissive]"
- "invalid use of 'this' at top level"
what i'm doing wrong with your idea?
http://forums.codeguru.com/showthrea...s-polymorphism
http://cboard.cprogramming.com/cplus...c-members.html
Victor Nijegorodov
-
September 10th, 2014, 02:45 PM
#9
Re: c++ 11 - can i override functions without re-declare them?
because you are trying to declare a test2:la when no such thing has been defined.
you can NEVER declare something without also defining it, I thought that was Obvious from your initial question. throwing a load of Sugar on it doesn't change that very fact.
-
September 10th, 2014, 11:01 PM
#10
Re: c++ 11 - can i override functions without re-declare them?
Originally Posted by OReubens
you can NEVER declare something without also defining it
I think you meant to write "you can NEVER define something without also declaring it" since all definitions are declarations, but not all declarations are definitions (e.g., forward declarations, or declaring a virtual member function to be pure virtual without defining it).
-
September 11th, 2014, 02:46 AM
#11
Re: c++ 11 - can i override functions without re-declare them?
Originally Posted by Cambalinho
see these sample:
Code:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
virtual void ola()
{
cout <<"hi" << endl;
}
test()
{
//nothing;
}
};
class test1 : public test
{
public:
test1()
{
//nothing;
}
};
void test1::ola()
{
cout << "hello" << endl;
}
int main()
{
//cout << test::a << endl;
return 0;
}
like you see, i don't re-declare the 'ola' function in 'test1' class, only in 'test' class.
the compiler tell me the 'ola' isn't member of 'test1'. in 'test' i put it 'virtual', but forgeting that, how can i override it without re-declare it?
You already asked the same question four months back here.
Was that discussion not enough?
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|