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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:16:16+00:00 2026-05-15T17:16:16+00:00

I have a base class( Base ) whose constructor takes a reference as argument.

  • 0

I have a base class(Base) whose constructor takes a reference as argument. In my derived class its constructor, I call the superclass-constructor and of course I need to pass a reference as argument. But I have to obtain that argument from a method of which the return type is by value…

I will give a short example:

class Base
{
public:
    Base(MyType &obj) { /* do something with the obj */}
};

class Derived : public Base
{
public:
    Derived(MyOtherType *otherType) :
         Base(otherType->getMyTypeObj()) // <--- Here is the error because (see *)
    {
         // * 
         // getMyTypeObj() returns a value and
         // the Base constructor wants a reference...
    }
};

class MyOtherType
{
public:
    MyType getMyTypeObj()
    {
         MyType obj;
         obj.setData( /* blah, blah, blah... Some data */);
         return obj; // Return by value to avoid the returned reference goes out of scope.
    }
};

How can I solve this problem?

  • 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-15T17:16:17+00:00Added an answer on May 15, 2026 at 5:16 pm

    Change the Base class to:
    class Base
    {
    public:
    Base(const MyType &obj) { /* do something with the obj */}
    };

    Update: If you want to modify obj you cannot obviously have a const reference. In that case you can either:

    1)Pass the parameter by value. That will have the overhead for the copy but avoid having to free it explicitly later.

    2) Change MyOtherType::getMyTypeObj() to

    MyType& MyOtherType::getMyTypeObj()
    {
        MyType* obj = new MyType();
        obj->setData( /* blah, blah, blah... Some data */);
        return *obj;
    

    }

    In this case, remember to delete the object after you are done with it.

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

Sidebar

Related Questions

Why does the following program crash? I have a base class whose destructor is
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have a base class A and a derived class B . class A
Let's say I have a System.Web.IHttpHandler whose base class is ASP.login_aspx whose base class
I have a ListBox whose datacontext is an: ObservableCollection<Item> Item is a base class,
I have a base class A and classes B and C are derived from
I have an interface with several events I have base class implementing the interface
I have a base class: class motorcycle { public: virtual int speed() { return
I have a base class ( car ) and a class that inherit the
I have a base class like this: public class BaseResponse { public string ErrorMessage

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.