Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Helper class uses main class' pointer

Reply
Thread Tools

Helper class uses main class' pointer

 
 
Nephi Immortal
Guest
Posts: n/a
 
      02-08-2011
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;
}
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      02-09-2011
On Feb 8, 7:03*pm, Nephi Immortal <immortalne...@gmail.com> wrote:
> * * * * 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:perator=( const B &r ) {
if( a == r.a )
return *this;
a->m_data = r.a->m_data;
return *this;
}

HTH
-- Saeed Amrollahi
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convenience class vs helper class Crocodile Java 0 09-22-2008 12:50 PM
What's the purpose of the helper cache and is there a way to just turn off helper cache-ing? kevin Ruby 1 07-21-2006 03:42 AM
Can I use DHCP helper on a Pix that uses NAT? tristan.rhodes@gmail.com Cisco 1 02-18-2006 03:23 PM
Main loop helper functions gamehack C Programming 6 01-29-2006 08:01 PM



Advertisments