Helper class uses main class' pointer

N

Nephi Immortal

I am trying to compare assignment operator to the main class’ pointer
while it is performing the task inside helper class’ operator=
function.
Please let me know if my code is to be correct design since I put
helper class inside main class.


typedef unsigned int size;

class A {
private:
struct B {
B() {}
~B() {}

B &operator=( const B &r ) {
if( a == r.a )
return *this;

a->m_data = r.a->m_data;
return *this;
}

B &operator=( const size ) {
return *this;
}

operator size() { return 0; }
operator size() const { return 0; }

A *a;
};

public:
A( size data ) : m_data( data ) {
b.a = this;
}
~A() {}

B &Go() {
return b;
}

private:
size m_data;
B b;
};


int main () {
A a( 0x25 ), a2( 0x7C );

a.Go() = a.Go(); // Skip assignment operator to the same object
a.Go() = a2.Go();

return 0;
}
 
S

Saeed Amrollahi

        I am trying to compare assignment operator to the main class’ pointer
while it is performing the task inside helper class’ operator=
function.
        Please let me know if my code is to be correct design since I put
helper class inside main class.

typedef unsigned int size;

class A {
private:
        struct B {
                B() {}
                ~B() {}

                B &operator=( const B &r ) {
                        if( a == r.a )
                                return *this;

                        a->m_data = r.a->m_data;
                        return *this;
                }

                B &operator=( const size ) {
                        return *this;
                }

                operator size() { return 0; }
                operator size() const { return 0; }

                A *a;
        };

public:
        A( size data ) : m_data( data ) {
                b.a = this;
        }
        ~A() {}

        B &Go() {
                return b;
        }

private:
        size m_data;
        B b;

};

int main () {
        A a( 0x25 ), a2( 0x7C );

        a.Go() = a.Go(); // Skip assignment operator to the same object
        a.Go() = a2.Go();

        return 0;

}

Hi Nephi

I try to say a few things, I guess may be helpful
and I hope they help you so:
1. The standard names for Main class and Helper class
are actually "Enclosing' and "Nested'. So A is Enclosing
and B is Nested class. At least these word are used in C++ standard
draft.
2. I think the main usage of nested class
is when you want to enclose some functionality
in an abstraction, which it is used by the enclosing
class and it isn't used directly by outsiders.
For example in Pimpl idiom or Handle/Body idiom
the body is a good candidate for nested classes.
Of course it is a general thing and there are exceptions.
In your code, I see no benefit come form nested class
you can decouple A and B and put them in 2 separate
classes. In that case you have to declare B a friend of A:
// a.h
struct B {
B &operator=( const B &r );
// ...
};

class A {
friend class B;
// ...
};

Also, you have to define assignment operator
of B in a .cpp file:


// a.cpp
#include "a.h"

B &B::eek:perator=( const B &r ) {
if( a == r.a )
return *this;
a->m_data = r.a->m_data;
return *this;
}

HTH
-- Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top