Ask coding questions

← Back to all posts
C++ Renaming base class members upon inheriting
xxpertHacker (854)

While you're on, @DynamicSquid, are you interested in looking into this?

Consider a class such as std::string, which has ::length(), and ::size(), which are functionally equivalent.

Let's say that I want to do this to my own class.

I have a class, A, which has a method A::f.

struct A {
    void f();

But, I have another class, B, which I want to have two methods, f, and g, aliasing A::f.

To alias A::f in the subclass, I can just use a using statement:

struct B : A {
    using A::f;

now the real problem comes: how can I reference A::f as g?

My naive attempt fails:

struct B : A {
    using A::f;

    using g = A::f; // A::g expected to be a type

Is there a way to alias member functions with different names?

DynamicSquid (4629)

My first thought was function pointers, but they may not be the best option. Then I found this SO answer, but I don't think it's what you need.

What do you need the alias for anyway? If you want to create two functionally equivalent functions, then just have one function call the other function

xxpertHacker (854)

@DynamicSquid I nearly died laughing at that video, but since class methods have a different ABI, they have a separate type, and different syntax.

void (A::* const f)() = A::f;

A object = A {};


Easily a thousand times more readable ;)

As for why I'd like to avoid calling it with a wrapper:
a) I'd like to avoid the code duplication & ugliness
b) I'd have to deal with forwarding types, reference-ness, const-ness, etc, to always perfectly match the underlying method's signature.

DynamicSquid (4629)

@xxpertHacker Well, I don't think a wrapper function is code duplication. If you're call a function from another function, then you're preventing code duplication.

I'll definitely have to think about this one for a bit. I can't think of anything right now that would even remotely solve your problem...

xxpertHacker (854)


I can't think of anything right now that would even remotely solve your problem...

Lol, in a way, now we're both in the same situation.

And I consider it duplication:

struct T {
    int x;

    int add(int x) const {
        return this->x + x;

    int add_alias(int x) const {
        return this->add(x);

T object = T { .x = 2 };


/* vs
struct A {
    // add() ...

    constexpr auto add_alias = A::add;

Call duplication, not body duplication.

But if you find anything, ping me.

If you don't, it's fine, I don't have a solution right now, so there would be no net change.

DynamicSquid (4629)

@xxpertHacker I actually don't mind call duplication, but that's just me

xxpertHacker (854)

@DynamicSquid Ha, you will when it's multiple levels deep...

void a();
void b() { a(); }
void c() { b(); }
void e() { c(); }

Toss in lambdas and stuff, and it becomes a convoluted mess.

This function is simple enough that it wouldn't matter, but in real code, it's just gross.

I see it way too often in JavaScript, not as often in C++.

DynamicSquid (4629)

@xxpertHacker Actually, a more important question is what is that dark blue and dark green doing against a dark grey background

Oh, I see now. I do admit, those can be ugly sometimes. Anyway, I gtg now, I'll see you later