Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:39:31+00:00 2026-06-14T13:39:31+00:00

I was wondering if there was a simple way to write an alias of

  • 0

I was wondering if there was a simple way to write an alias of a c++ class function. For instance, if I have some list container object, a logical function would be

    int list::length() { return len; }

But another logical alias that programmers might use could be

    int list::size() { return len; }

So, instead of writing both functions with their full body, is there any way to make list::size() an alias of list::length() such that it isn’t a duplicate when compiled, but rather references the same function?

I’ve read that you can do this with #define, but I don’t want to cause any confusion with other code-names somewhere totally out of scope (i.e. a ‘size’ variable).
I’ve also read that function pointers can fix it, but that isn’t exactly an alias (since it has to apply de-referencing), nor can function pointers be given a declaration, giving it a confusing help-line to users (I would think), plus the confusion if ever I need to nest my code inside another object (I have to adjust the scope).

One of my guesses is, will the following be taken as a direct function alias by most optimizing compilers:

    inline int list::length() { return len; }
    inline int list::size() { return length(); }

Or, is there any strict ‘alias’ syntax for c++? (I couldn’t find any – wasn’t sure)
So then, what would be the most efficient way of doing this?

EDIT: I’ve accepted the answer simply to wrap up the question, since it’s only a curiosity of mine. Anyone with good information, please add comments or answer, and I may even change my answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:39:32+00:00Added an answer on June 14, 2026 at 1:39 pm

    I would not use the preprocessor and #define to do this. In general preprocessor should be a last resort in C++. See this C++ FAQ on inline functions which also contains a section on the various evils of using macros of the preprocessor.

    The approach I would use would be to have a function that will have several different aliases with a more complicated function and interface you would do something like the following:

    int list::length(string xString, int iValue) {
      int iReturnValue = 0;  // init the return value
      //  do stuff with xString and iValue and other things
      return iReturnValue;
    }
    

    Then do something like the following for an alias.

    inline int list::size(string xString, int iValue) {return length(xString, iValue);}
    

    The inline should basically just replace the alias with the actual function call.

    See also this stack overflow posting Giving a function implementation more than one name. It provides some reasons why you might not want to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if there is a simple way to allow a JQuery datatable
Hai. I was making this simple string class and was wondering if there was
There are some different opinions about simple inner classes, so I was wondering if
I'm wondering if there is a way to write MIPS assembly language file (just
Just wondering if there is a better way to write this CSS? It repeats
I'm wondering if there's any way to populate a dictionary such that you have
I'm wondering if there's a simple way for a Word macro to determine which
There is this simple function which I have used with C++ in the past
I am wondering if there is a simple snippet which converts links of any
I was wondering whether there is a simple PHP library to test the loading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.