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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:59:14+00:00 2026-05-25T23:59:14+00:00

Can anyone tell me how I can pass an object to a C++ function?

  • 0

Can anyone tell me how I can pass an object to a C++ function?

Any better solution than mine?

#include<iostream>
using namespace std;
class abc
{
      int a;
      public:
             void input(int a1)
             {
                  a=a1;
             }
             int display()
             {
                  return(a);
             }   
};

void show(abc S)
{
    cout<<S.display();
}

int main()
{
    abc a;
    a.input(10);
    show(a);
    system("pause");
    return 0;
}
  • 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-05-25T23:59:14+00:00Added an answer on May 25, 2026 at 11:59 pm

    You can pass by value, by reference or by pointer. Your example is passing by value.

    Reference

    void show(abc& S)
    {
        cout<<S.display();
    }
    

    Or, better yet since you don’t modify it make it int display() const and use:

    void show(const abc& S)
    {
        cout<<S.display();
    }
    

    This is normally my “default” choice for passing objects, since it avoids a copy and can’t be NULL.

    Pointer

    void show(abc *S)
    {
        cout<<S->display();
    }
    

    Call using:

    show(&a);
    

    Normally I’d only use pointer over reference if I deliberately wanted to allow the pointer to be NULL.

    Value

    Your original example passes by value. Here you effectively make a local copy of the object you are passing. For large objects that can be slow and it also has the side effect that any changes you make will be made on the copy of the object and not the original. I’d normally only use pass by value where I’m specifically looking to make a local copy.

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

Sidebar

Related Questions

Can anyone tell me how to pass parameters to a function in another controller
Can anyone tell me what the need/advantage is to using a web service with
Can anyone tell me how to create a scheduled task using powershell that runs
You can pass a function pointer, function object (or boost lambda) to std::sort to
anyone can tell me, how to pass arguments in cakephp $this->requestAction(...)?
Can anyone tell me how to pass JavaScript values to Scriptlet in JSP?
When using a Trace.Listener can anyone tell me why the Trace.Write(string message, string category)
Can anyone please tell me what I should be using and why this method
Can anyone tell me how I can display a status message like 12 seconds
Can anyone tell me what is the memory usage overhead associated with PHP opcode

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.