Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to implement the pointer, but was not able to reproduce it.

Please help me to go ahead...

C++
#include<iostream>
using namespace std;
class A
{
public:
int a1;
int *b1;
A(int a, int b)
{
cout<<"Constructor\n";
a1 = a;
b1 = new int(b);
}
A(const A& a)
{
cout<<"Copy constructor\n";
a1 = a.a1;
b1 = new int;
b1 = (a.b1);
}
};
#include<stdio.h>
 
int main()
{
A *a = new A(88, 56);
A *a1;
a1 = a;
printf("Address of a : %p\n", a);
printf("Address of a1 : %p\n", a1);


What I have tried:

I tried with static allocation, but using pointer, am missing something. Please help!
Posted
Updated 12-Oct-23 9:38am
v2

This has been heavily revised. I added a default constructor, an assignment constructor, and an assignment method :
C++
class A
{
public:
    int   a1 { 0 };
    int * b1 { nullptr };

    A() {}                  // default constructor

    A( int x, int y )       // assignment constructor
    {
        Set( x, y );
    }

    A( const A & other )    // copy constructor
    {
        Set( other.a1, * other.b1 );
    }

    // copy operator

    A & operator = ( const A & other )
    {
        // prevent self-assignment

        if( this != & other )
        {
            // do copy operation

            Set( other.a1, * other.b1 );
        }
        return *this;
    }

    void Set( int x, int y )   // assignment method
    {
        a1 = x;
        if( b1 )
            delete b1;
        b1 = new int( y );
    }
};


int main()
{
    A a;
    A b( 42, 43 );

    a = b;
    trace( "a.a1 is %d, a.b1 is %d\n", a.a1, * a.b1 );

    A c( 56, 57 );
    trace( "c.a1 is %d, c.b1 is %d\n", c.a1, * c.b1 );

    A d( a );
    trace( "d.a1 is %d, d.b1 is %d\n", d.a1, * a.b1 );

    return 0;
}
New assignments need to re-allocate the b1 pointer if there is one so it MUST be initialized appropriately as shown. That is one reason why initialization is so important.

Note how all constructors are routed through the Set method. This can be a useful tactic to use, especially if your members are not public.

BTW - trace is my own output function that I use with GUI programs. You can replace it with printf or use cout redirection like your were.
 
Share this answer
 
v3
Comments
mathiv327 12-Oct-23 11:52am    
for me, it's not working.... Below i have tried....!

#include<iostream>
using namespace std;

class A
{
public:
int* a1 { NULL };
A(int a):a1(new int(a)) {
cout<<"Normal Constructor\n";
}

A(const A& a)
{
cout<<"Copy Constructor\n";
a1 = new int(*(a.a1));
}

A& operator = (const A& a)
{
cout<<"Assignment Constructor\n";
if(this != &a)
{
if(a1)
delete a1;
a1 = new int ( *a.a1 );
}
return *this;
}
};


int main()
{
A* a = new A(33);
A* b;
b = a;
*(a->a1) = 44;
cout<<*(a->a1)<<endl;
cout<<"val="" :="" "<<(*b-="">a1)<
Rick York 12-Oct-23 13:21pm    
You have a few syntax errors there. Please see my revisions. They have been verified to work correctly.
mathiv327 12-Oct-23 14:15pm    
thank you, so much
Rick York 12-Oct-23 18:30pm    
If these solutions solved your problems you should accept them so your question is consider answered. Upvotes are also nice. :)
The statement A *a1 only defines a pointer to an object, but does not allocate any memory to the pointer. There's at least two options that I can think of to invoke a copy constructor when using pointers to objects:
1) create the new pointer object using by dereferencing the poiner
C++
A *a2 = new A(*a)

2) create a pointer version of the copy constructor
C++
class A {
  // ...
  A(const A *a) {
    // construct new A object
  }
};

int main() 
{
    A *a1 = new A(1, 2);
    A  *a2 = new A(a1);
} 
If the object is complex, then a private member function that does the work might be a good idea.
 
Share this answer
 
v3
Comments
mathiv327 12-Oct-23 12:06pm    
gets compilation error, like below...
error: expected type-specifier before ‘;’ token
35 | A *b = new(a);
k5054 12-Oct-23 12:26pm    
Oops. Typo on my part. Should be
A *b = new A(a);

Updated solution to reflect this.
mathiv327 12-Oct-23 14:15pm    
thank you, so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900