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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:20:36+00:00 2026-06-10T22:20:36+00:00

I’ve read a quick tutorial aimed for Java developers who want to learn C++.

  • 0

I’ve read a quick tutorial aimed for Java developers who want to learn C++. It only explained the basic principles (and syntax) of C++ (I guess). At first I thought, that I had understand everything completely, but while programming C++ something came up that’s not really clear to me.

Whats the difference between …

ExampleClass* doSomething(ExampleClass* ec) {}

and

ExampleClass* doSomething(ExampleClass& ec) {}

and

ExampleClass& doSomething(ExampleClass* ec) {}

and

ExampleClass& doSomething(ExampleClass& ec) {}

?

  • 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-10T22:20:37+00:00Added an answer on June 10, 2026 at 10:20 pm

    Pointers and references have similarities. They both point to or reference an object without having space to represent the object value themselves. The pointer however, explicitly uses the * to dereference (as an unary operator), and & to get the address of an object. The * is also used in a different context, to specify a pointer type.

    The reference is a somewhat safer “automatic” pointer. A reference however is immutable in the sense that it cannot be later changed to point to something else. A reference also uses the & symbol, but in a different context. Instead of being an unary operator, it is used to specify a reference type.

    The 1st example takes a pointer to an ExampleClass and returns a pointer to an ExampleClass object. Eg. you might say:

    ExampleClass* doSomething(ExampleClass* ec) {
      return ec;
    }
    ExampleClass * pointer = new ExampleClass();
    ExampleClass * anotherpointer = doSomething(pointer);
    

    In contrast, this following takes a reference to ExampleClass instead. A reference is like a pointer, but it means you don’t pass something of pointer type, just pass it straight through, eg:

    ExampleClass* doSomething(ExampleClass& ec) {
      return &ec; // & unary operator - get the address of ec
    }
    
    ExampleClass obj = ExampleClass();
    ExampleClass* pointer = doSomething(obj); // it will automatically get a reference to the input object
    

    The next example takes a pointer and returns a reference instead (notice that the return type is not a pointer):

    ExampleClass& doSomething(ExampleClass* ec) {
      // note ec of is type ExampleClass*
      // *ec is of type ExampleClass
      return *ec; // returns a reference to whatever the pointer points to
    }
    ExampleClass * pointer = new ExampleClass();
    ExampleClass& myobj = doSomething(pointer);
    

    You just pass it to a reference object (which points to the object given by the function, ie. doees not make a copy). Note that in this case, the function should take care of making sure there is space allocated for the object, and does not need to explicitly reference (with *) the object in the return statement.

    I think you can work out the last example:

    ExampleClass& doSomething(ExampleClass& ec) {}
    

    Do note that when returning a reference, you must ensure that the object that is referenced is allocated space outside of the context of a function (eg. as a global, or static local variable), so that it is not destroyed.

    You should not return a reference to a local variable, which will be destroyed when the function exits, eg:

    ExampleClass& doSomething(ExampleClass* ec) {
      ExampleClass copy = *ec;
      return copy; // WARNING: returning reference to object that will be destroyed
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
i want to parse a xhtml file and display in UITableView. what is the

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.