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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:57:32+00:00 2026-05-27T02:57:32+00:00

I have this code in a library: class Parent { //some data and functions

  • 0

I have this code in a library:

class Parent
{
    //some data and functions
};

void myfunc(Parent& ref);

and I want to do this code in my application:

class Child : public Parent
{
   // some other data and functions
   void dostuff()
   {
       myfunc(*this);
   }
};

Is it safe to pass *this? (no slicing, no copying, …)
Is it better to call myfunc like this:

myfunc( * ((Parent*)this) )

Note that I don’t have control on what happens inside myfunc, in some cases I don’t even know what happens inside there.

I used passing-parent-by-pointer many times and I am used to it, but have never used passing-parent-by-reference before.

  • 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-27T02:57:33+00:00Added an answer on May 27, 2026 at 2:57 am

    myfunc(*this) is fine, so long as myfunc is declared to take a reference — which it is.

    This will not copy the object. It will pass a reference to the original object. Furthermore, it will not slice the object. The reference will be of type Base&, but the object to which it refers will be unchanged.

    Just so you know, if you were then to call polymorphic (eg, virtual) methods on this Base&, polymorphism will still work right and do what you’d expect — just like if you were to call through a pointer. In other words:

    Base& b = *derived;
    b.SomeVirtualFoo();
    

    …will have the same effect as:

    Base* b = derived;
    b->SomeVirtualFoo();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this library code, see... class Thing { public: class Obj {
I have this small code library that I'm considering releasing into Open Source. I
I have used code like this: http://msdn.microsoft.com/en-us/library/dw70f090.aspx to access database before when working in
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);
I have the following code: public class Parent { public string MyField { get;
Let's say I have this C++ code: void exampleFunction () { // #1 cout
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {

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.