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 8022029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:09:28+00:00 2026-06-04T22:09:28+00:00

I want to override a function with same parameter type, but with different logical

  • 0

I want to override a function with same parameter type, but with different logical meaning. I’ve tried something like:

class T
{
};

typedef T Age;
typedef T Height;
typedef T Weight;

void foo(Age a){}
void foo(Height a){}
void foo(Weight a){}

but I have build errors:
error C2084: function ‘void foo(Age)’ already has a body

One solution would be:

class Age : public T{};
class Height : public T{};
class Weight : public T{};

but I don’t want to fill my namespace with new classes only for this purpose.

How may I achieve this without using derived classes?

EDIT: My code is in a cpp file, I don’t use headers. This is just a simple example. Full cpp content is here:

class T
{
};

typedef T Age;
typedef T Height;
typedef T Weight;

void foo(Age a){}
void foo(Height a){}
void foo(Weight a){}

int main()
{
    return 0;
}

Error message:
.cpp(10): error C2084: function ‘void foo(Age)’ already has a body

  • 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-04T22:09:29+00:00Added an answer on June 4, 2026 at 10:09 pm

    You could use templates as:

    //1. First define T as class template
    template<typename U>
    class T
    {
     public:
        typedef U tag_type;  //you may need this to inspect the type
        //...
    };
    
    //2. then define some tags
    struct tag_age{};
    struct tag_height{};
    struct tag_weight{};
    
    //3. then define typedefs
    typedef T<tag_age> Age;
    typedef T<tag_height> Height;
    typedef T<tag_weight> Weight;
    
    //4. then function overloads
    void foo(Age a){}
    void foo(Height a){}
    void foo(Weight a){}
    

    In this way, each of the typedefs are different types, and cannot be implicitly converted into other types, unless you allow this functionality explicitly in the defintion of the class template T.

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

Sidebar

Related Questions

Working in a symfony model, I want to override a function and call the
I'm attempting to override and implement my own TabExpansion. In the function I want
I want to override the tree_id field as following: Given: class Thing(MPTTModel): thing_id =
I want to have the same static variable with a different value depending on
I have a base class that I want to look like this: class B
I'm writing an extension to the Flex DataGridColumn class. I want to override the
I want to override the SubmitChanges() method for my model. Whern I try to
I want to override the Tostring() method for changing some characters. Is it possible?
I want to override the validation_errors() method of the form helper in CodeIgniter for
I want to override the selection behavior of ListBox Items. We can traverse through

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.