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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:10:52+00:00 2026-05-30T16:10:52+00:00

foo.h #include "class1.h" class foo{ private: class1* class1ObjectPointer; public: foo(); virtual ~foo(); void foomethod();

  • 0

foo.h

#include "class1.h"

class foo{

    private:
        class1* class1ObjectPointer;
    
    public:
        foo();
        virtual ~foo();
    
        void foomethod();

}

foo.cpp (VERSION 1)

#include "foo.h"

foo::foo()
{
    this->class1ObjectPointer = new class1();
}


foo::~foo()
{
    if( this->class1ObjectPointer != NULL ){

        delete class1ObjectPointer;
        this->class1ObjectPointer = NULL;
    }
}


foo::foomethod(){

    *(this->class1ObjectPointer) = some_method(); 
    //suppose some method returns an object of class1

}

foo.cpp (VERSION 2)

#include "foo.h"

foo::foo()
{
    this->class1ObjectPointer = NULL;
}


foo::~foo()
{
    if( this->class1ObjectPointer != NULL ){

        delete class1ObjectPointer;
        this->class1ObjectPointer = NULL;
    }
}


foo::foomethod(){

    class1 object;
    object = some_method(); 
    //suppose some method returns an object of class1
    this->class1ObjectPointer = new class1(object); // copy constructor
}

Which pointer assignment is better in the following cases:

  1. objects of class1 always have fixed size (e.g. a class with fixed variables in it)
  2. objects of class1 may have members with variable size (e.g. a class that has a matrix in it which can potentially have different sizes)

Would you suggest any other and better way of doing what I did in these snippets?

  • 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-30T16:10:54+00:00Added an answer on May 30, 2026 at 4:10 pm

    It’s probably better (more efficient, and easier to avoid memory leaks) to reassign the object per your first example than to allocate a new object – as demonstrated by the memory leak in your second example.

    It looks very much like you don’t need a pointer at all; just embed an object in your class:

    class foo {
    private:
        class1 class1Object;
    
    public:
        // implicit constructors, assignment and destructor are fine
        void foomethod() {class1Object = some_method();}
    };
    

    Your second situation makes no sense – objects of a particular class are all the same size.

    If you really do need to store a pointer (perhaps because you need polymorphism), then the simplest way to handle it is with a smart pointer:

    class foo {
    private:
        std::unique_ptr<class1> class1ObjectPointer;
    
    public:
        foo() : class1ObjectPointer(new class1) {}
        // implicit copying and destructor are fine
    
        foomethod() {*class1ObjectPointer = some_method();}
        // or, if not assignable, class1ObjectPointer.reset(new class1(*some_method()));
    };
    

    If you really want to manage it yourself, then you’ll need to override the default copy constructor and copy-assignment operator per the Rule of Three, make sure you delete the old object when reassigning the pointer, and be careful about exception safety. And there’s no need to either check for NULL before deleting, or to assign NULL in the destructor.

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

Sidebar

Related Questions

#include <iostream> template <class T> void foo(T) { std::cout << "foo(T)" << std::endl; }
When compiling : #include <vector> template<class T> class foo { void bar() { std::vector<T>
Consider this: class Foo{ void func1(){ /*func1 code*/ } void func2(){ /*func2 code*/ }
The following code: foo.h #include bar.h class foo{ public: enum my_enum_type { ONE, TWO,
How can I include foo() function of foo.c in this small program (sorry for
FILE #1 (foo.h): #ifndef FOO_H_ #define FOO_H_ #include baseclass.h #include bar.h class Bar; class
#include<stdio.h> void foo(int **arr) { arr[1][1]++; } main() { int arr[20][20]; printf(%d\n,arr[1][1]); foo((int**)arr); printf(%d\n,arr[1][1]);
when accessing foo() of base using derived class's object. #include <iostream> class base {
Consider the following code: #include <iostream> struct foo { // (a): void bar() {
Using scons I can easily set my include paths: env.Append( CPPPATH=['foo'] ) This passes

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.