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

  • Home
  • SEARCH
  • 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 8613413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:52:11+00:00 2026-06-12T04:52:11+00:00

My question is related to the arguments in a C++ function. Sometimes, you may

  • 0

My question is related to the arguments in a C++ function. Sometimes, you may expect that the one function can accept different kinds of arguments, and as far as I am considered this can be realized in two ways. One is to use the C++ new feature: function overloading (Polymorphism), and the other is to use the ‘C’ function way, which is illustrated in the following examples:

struct type0
{
    int a;
};

struct type1
{
    int a;
    int b;
};

struct type2
{
    int a;
    int b;
    int c;
};

void fun(int type, void *arg_structure)
{
    switch (type)
    {
    case 0:
        {
            struct type0 *mytype = (struct type0 *)(arg_structure);
            cout<<"a = "<<mytype->a<<endl;
            break;
        }

    case 1:
        {
            struct type1 * mytype= (struct type1 *)(arg_structure);
            cout<<"b = "<<mytype->b<<endl;
            break;
        }
    case 2:
        {
            struct type2 *mytype = (struct type2 *)(arg_structure);
            cout<<"c = "<<mytype->c<<endl;
            break;
        }
    default:
        break;
    }
}

int main () 
{
    struct type2 temp;
    temp.a = 1;
    temp.b = 2;
    temp.c = 3;
    fun(2,(void*)(&temp));

    return 0;
}

My question is: are there other ways of obtaining a changeable function argument structure in C++?Thanks!

  • 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-12T04:52:12+00:00Added an answer on June 12, 2026 at 4:52 am

    As you ask for ‘changeable argument structure’ and not ‘changeable argument type’, I would assume that you are asking for flexibility in terms of types AND number of arguments.

    If so, you can use variadic functions:

    void fn ( int cnt_params, ... ) {...}
    

    or, if you have a compiler supporting C++11, variadic templates:

    template <typename T, typename ...P>
    void fn (T t, P ...p) {....}
    

    Otherwise, you can use overloading or templates, as others have already suggested.

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

Sidebar

Related Questions

Another question related to this one . I have a List<SortableObjects> that is the
I have a simple question related to one-line programming. First an example: function test(a)
I have a question related to this one . I don't want to do
I have a question related to this one : I'm trying to attach an
This question is related to one I asked the other day which I got
This question is related to the one here: configurable dependencies with easy to mock
I found a question (and an answer) related to sorl-thumbnail that looks like a
This question is related to my another question- boost::bind return a function object which
Possible Duplicate: passing 2D array to function My question is related to passing an
Question related to PHP memory-handling from someone not yet very experienced in PHP: If

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.