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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:05:01+00:00 2026-05-28T01:05:01+00:00

Possible Duplicate: How to pass objects to functions in C++? Operator & and *

  • 0

Possible Duplicate:
How to pass objects to functions in C++?
Operator & and * at function prototype in class

#include <iostream>
using namespace std;

class C {
  public:
    int isSelf (C& param);
};

bool C::isSelf (C& param)
{
  if (&param == this) return true;
  else return false;
}

int main () {
  C a;
  C* b = &a;
  cout << boolalpha << b->isSelf(a) << endl;
  return 0;
}

This code works. But it seems to me that b->isSelf(a) should really be b -> isSelf(&a) because isSelf expects an address of type C?!

[EDIT]
Additional questions:

1) Is there a way to implement this isSelf function using pass by value?
2) Are the implementation using pass by reference and pass by pointer correct?

bool C::isSelf1(const C &c)
{
    if (&c == this) {
        return true;
    } else {
        return false;
    }
}

bool C::isSelf2(C c)
{
    if (&c == this) {
        return true;
    } else {
        return false;
    }
}

bool C::isSelf3(C * c)
{
    if (c == this) {
        return true;
    } else {
        return false;
    }
}

int main ()
{
    C c1 (2);
    C * c2 = &c1;
    cout << boolalpha;
    cout << c2 -> isSelf1(c1) << endl; // pass by reference
    cout << c2 -> isSelf2(c1) << endl; // pass by value
    cout << c2 -> isSelf3(&c1) << endl;// pass by pointer
    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-28T01:05:02+00:00Added an answer on May 28, 2026 at 1:05 am

    There is a difference between:

    bool C::isSelf (C& param)  // pass by 'reference'
                   ^^^
    

    and

    bool C::isSelf (C* param)  // pass by 'address'
                   ^^^
    

    So in 2nd version address of C object (i.e. C*) is expected and not in 1st version.

    Also note that, internally 1st and 2nd versions might be implemented similarly; but there is a syntax difference. There is a 3rd version:

    bool C::isSelf (C param)  // pass by value
                   ^^^
    

    For edited question:

    1) Is there a way to implement this isSelf() function using pass by value?

    Not possible for your given requirement. Because it creates a new value and that would never match. Remove pass by value version from your code.

    Apart from that, in general you should choose either pass by value or pass by reference, for any function. You can’t have both, because function call syntax for both of them would be same, which will result in ambiguity.

    2) Are the implementation using pass by reference and pass by pointer correct?

    They are correct, but you can simplify them as:

    bool C::isSelf(const C &c) const // don't give postfix '1', simply overload
    {                          ^^^^^
      return (&c == this);
    }
    
    bool C::isSelf(C* const c) const // don't give postfix '3', simply overload
    {                          ^^^^^
      return (c == this);
    }
    

    Also, see the const correct-ness syntax of doing such things.

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

Sidebar

Related Questions

Possible Duplicate: FAQ : How to pass objects to functions in C++? Pointer vs.
Possible Duplicate: How to pass objects to functions in C++? My main program gives
Possible Duplicates: “const T &arg” vs. “T arg” How to pass objects to functions
Possible Duplicate: How do you pass a function as a parameter in C? Is
Possible Duplicate: Multiple arguments to function called by pthread_create()? How to pass more than
Possible Duplicate: SWIG Java Retaining Class information of the objects bouncing from C++ Question
Possible Duplicate: PHP function with unlimited number of parameters How to pass array as
Possible Duplicate: How to pass text in a textbox to javascript function? I need
Possible Duplicate: pass inputbox value as a querystring to URL <input name=KeywordBox class=BasicSearchInputBox type=text
Possible Duplicate: Pass a data.frame column name to a function I am trying to

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.