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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:33:13+00:00 2026-06-13T00:33:13+00:00

Possible Duplicate: Pointer vs. Reference I am wondering if there any benefits in using

  • 0

Possible Duplicate:
Pointer vs. Reference

I am wondering if there any benefits in using references instead of pointers on some interface. Being more specific let’s consider small example:

struct MyInterface {
   virtual ~MyInterface() {}
   virtual void f() = 0;
};

class MyClass : public MyInterface
{
   virtual void f()
   {
      std::cout << "F()" << std::endl;
   }
};


void myFunction(MyInterface& obj);

int main(int argc, char *argv[])
{
   MyInterface* pObj = new MyClass;

   myFunction(*pObj);

   delete pObj;

   getchar();
   return 0;
}

void myFunction(MyInterface& obj)
{
   obj.f();
}

In myFunction instance of MyClass can be passed as a pointer, what is written in many books. My question is what can be considered as a good practice (pointer or reference) and what is more efficient?

Sorry if this question somehow was asked previously.

  • 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-13T00:33:14+00:00Added an answer on June 13, 2026 at 12:33 am

    Once a value is passed, passing a reference is, performance wise and semantics wise, the same as passing a pointer.
    In the example you wrote, you are passing a reference obtained from a pointer, so the limitations imposed by reference-input function are invalidated here. Indeed, if you wrote

    MyInterface* pObj = 0;
    myFunction(*pObj);
    

    You would get a runtime error. But the error is because you are doing

    myFunction( *0 );
    

    i.e., because you are dereferencing a null pointer, not because you are passing the zero value. In fact,

    myFunction( 0 );
    

    will give you a compile-time exception (which is great).

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

Sidebar

Related Questions

Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there any
Possible Duplicate: Does using references instead of pointers, resolve memory leaks in C++? When
Possible Duplicate: Pointers in java Why java is not using pointer though C# and
Possible Duplicate: Are there gotchas using varargs with reference parameters Hi, I have a
Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there a
Possible Duplicates: When pass-by-pointer is preferred to pass-by-reference in C++? Are there benefits of
Possible Duplicate: Difference between pointer variable and reference variable in C++ When should I
Possible Duplicate: Difference between pointer variable and reference variable in C++ I am reading
Possible Duplicate: What are the differences between pointer variable and reference variable in C++?
Possible Duplicate: C++: When to use References vs. Pointers I'm pretty new programmer 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.