overrride\change functions

can i create a functions in base class and edit them in new class(derived from the base class) without redeclare them?
(i'm loking for a long time about these, but seems not possible... but maybe the C++11 can help me ;) )

i have read something that i can disable the redeclaration, what can anyone tell me?
Last edited on
I'm not sure what you're talking about.

If you want to override a base class function in a derived class, you just need to declare the function in the base class as virtual:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Base
{
    public:
        virtual void foo();
};
void Base::foo()
{
    // Blah blah...
}
class Derived: public Base
{
    public:
        void foo(); // Overrides Base::foo
};
void Derived::foo()
{
    // Blah blah...
    // If you want to call the base class's 'foo' function, use this:
    Base::foo();
}
(You should probably make the destructors virtual, too.)

If you're trying to make it so you don't have to declare the function at all (as in, so that you don't have to type void foo(); again) in the derived class...I'm not sure why you would want to do that. Where would the code for the function go?

What C++11 added was this:
1
2
3
4
5
class Derived: public Base
{
    public:
        void foo() override; // The 'override' tells the compiler that this overrides 'Base::foo'
};
In our case, this is exactly the same as above, though it conveys your intentions more clearly and also helps you catch an error in case your declaration of Derived::foo doesn't actually override the one in the base class (for instance, if the parameters are different or something).

C++11 also added this:
1
2
3
4
5
6
7
8
9
10
class Derived: public Base
{
    public:
        void foo() final; // The 'final' tells the compiler that you can't override this function
};
class MoreDerived: public Derived
{
    public:
        void foo() override; // Error: overriding a 'final' function
};
I think that might be what you're referring to when you say "disable the redeclaration".

900!
Last edited on
sorry... i mean like these:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class b
{
public:
	void print() override {}

};

class d : public b
{

};

void d::print()
{
    cout << "hello";
}
No, you have to declare it in d as well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class b
{
public:
    virtual void print();
};

class d: public b
{
public:
    void print() override; // The 'override' part is technically optional here
};

void d::print()
{
    cout << "hello";
}
It's not that hard to stick the declaration in the derived class as well...at worst, it's just a copy/paste.
my objective is avoid the line 10, independent of the line 4(virtual or other keyword). what you can tell me?
You can't do it.

It's just like trying to do this:
1
2
3
4
5
6
7
8
9
10
// Forget the inheritance for a moment...
class MyClass
{

};

void MyClass::foo() // "Skipping" the declaration inside the definition of 'myClass'...
{
    std::cout << "Hi there!";
}
What you could do is put the whole function inside the class declaration:
1
2
3
4
5
6
7
8
class d: public b
{
public:
    void print() override
    {
        cout << "hello";
    }
};
But I wouldn't do that, unless it's a really small function (like, a line or two).

But why do you want to avoid putting the declaration in anyways?
think in these way: if the functions in class b are for be changed\overrrided, why re-declare them in class d?
So that the compiler can know to call the overridden one rather than the original. For example, you can still do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class B {
public:
    void print() { std::cout << "Base\n"; }
};

class D : public B {
public:
    void print() { std::cout << "Derived\n"; }
};

int main() {
    D d;
    d.print();
    d.B::print();

    return 0;
}
Derived
Base

http://coliru.stacked-crooked.com/a/85fe4819db79e151
Topic archived. No new replies allowed.