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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:48:26+00:00 2026-05-15T14:48:26+00:00

I’m very new to C++ and I’m currently learning it. I got a few

  • 0

I’m very new to C++ and I’m currently learning it. I got a few questions..

  1. What is the differences between void DoSomething(const Foo& foo) and void DoSomething(Foo foo)? If we don’t specify & then the instance of Foo will be passed by value ( not reference ). It will be the same as having const + & in argument except no checking at compile-time. So, Why does having const + & become the best practice over the argument without & and const?

    In C#, passing the object is "by reference" but seems like it’s not in C++.

  2. The book that I’m reading said that Member functions pass the implicit parameter by reference..

    Could anyone give me the sample of implicit parameter and by reference? I know that if we want to pass the object by reference, we need to use & (e.g. Foo(Person& p) ) but how come C++ pass the object by reference for implicit parameter? I read that implicit parameter in C++ is like Contructor(string str) : strMemberVariable(str) {} …

  3. Is the array the only that pass by reference in C++?

  4. Why can’t I use Foo fInstance in Foo class?

Example:

class Foo {

public:    
    Foo() { }

    Foo(const Foo& f) : fInstance(f) {   }  

    Foo fInstance;      
};

Thanks in advance.

  • 1 1 Answer
  • 1 View
  • 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-15T14:48:26+00:00Added an answer on May 15, 2026 at 2:48 pm

    1 What is the differences between void DoSomething(const Foo& foo) and void DoSomething(Foo foo)? If we don’t specify & then the instance of Foo will be passed by value ( not reference ). It will be the same as having const + & in argument except no checking at compile-time. So, Why does having const + & become the best practice over the argument without & and const?

    In C#, passing the object is "by reference" but seems like it’s not in C++.

    There are several differences, in order of importance:

    • If the object Foo cannot be copied, you need to pass it by reference
    • If the object Foo is a base class, you should get it by reference so that users can call your functions with derived classes
    • The value of the actual object might change even though you hold a const reference to it
    • Efficiency, copying user types might be expensive, but compilers may be smart enough to figure it out so…

    2 The book that I’m reading said that Member functions pass the implicit parameter by reference..

    Could anyone give me the sample of implicit parameter and by reference? I know that if we want to pass the object by reference, we need to use & (e.g. Foo(Person& p) ) but how come C++ pass the object by reference for implicit parameter? I read that implicit parameter in C++ is like Contructor(string str) : strMemberVariable(str) {} …

    By implicit parameter you should understand this, that is the object itself. It is effectively passed by reference since you can modify its state in the member function.

    Following Konrad‘s remark: note that this itself is not passed by reference, this is a reference (pointer) to the object, but is passed by value. You can’t change the memory address of your object as you wish 😉

    3 Is the array the only that pass by reference in C++?

    They aren’t. You will see changes to the elements of the array, but the array (structure) will not change.

    Following FredOverflow‘s remark, an illustration:

    void fun(int* p, size_t size);
    
    int main(int argc, char* argv[])
    {
      int array[15];
      fun(array, 15);
    }
    

    We don’t know what fun does, it will probably change some elements of array, but whatever its action, array will remain an Array of 15 integers: the content changes, the structure does not.

    As a result, to change array we need another declaration:

    void changer(int*& array, size_t& size);
    

    This way we can change both the content and the structure (and pass back the new size too). And of course we can only call this function with an array that was dynamically allocated.

    4 Why can’t I use Foo fInstance in Foo class?

    Because that’s infinite recursion. Think about it from a compiler point of view, and try to guess the size of Foo. The size of Foo is the sum of the sizes of its attributes, plus possibly some padding and type information. Also, an object size is at least 1 so that it can be addressed. So, if Foo has a Foo, what’s its size 🙂 ?

    The usual solution is to use a smart pointer:

    class Foo
    {
    public:
    
    private:
      std::unique_ptr<Foo> mInstance;
    };
    

    Because the size of a pointer does not depend on the size of the object pointed to, so there is not recursion going on here 🙂

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a jquery bug and I've been looking for hours now, I can't

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.