gcc

Overloading inherited functions in C++

Although overloading an inherited function implies that the (now) overloaded function of the base class will not be visible anymore in the subclass, by using the using keyword, you can make it accessible again:

#include <iostream>
#include <string>

using namespace std;

class A {
    public:
        void x() { cerr <<"x"; }
};

class B : public A {
    public:
        void x(string s) { cerr <<s; }
        using A::x;
};

int main() {
    B b;
    b.x();  // without using A::x in B's declaration, this line would fail to compile
}

Initialization of static data members of non-integral type in C++

class A
{
private:
static const int X = 42; //OK
static const char Y = 'Y'; //OK
static const char Z[]; //if you write here ="message" before the semicolon, you get a compile error saying 'invalid in-class initialization of static data member of non-integral type'
public:
//..
};
const char A::msg [] = "hello"; //this is the way it goes

Another tip: I could have defined Z as char *, but I didn't because it would waste resources. Because it is a constant string, I do not need to change where it points to, so I declare it as a simple array, and that way I skip the dereferencing of a pointer every time it get accessed.

Syndicate content