Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Attribute Setting Methods

Reply
Thread Tools

Attribute Setting Methods

 
 
kjell@cablescan.com
Guest
Posts: n/a
 
      01-16-2008
Hi,

I wonder if someone may have a solution to a small problem I'm
experiencing with a C++ program? I'm have two classes in my
application that provides methods for setting parameters in the
classes. These methods returns a pointer to the class itself so that
you can use the return value to call another attribute setting method
on the same line. This is what my program look like:

#include <iostream>



using namespace std;



class CBase

{

public:

CBase* SetParameterA(const int nA)

{

m_nA = nA;

return this;

};



private:

int m_nA;

};




class CSub: public CBase

{

public:

CSub* SetParameterB(const int nB)

{

m_nB = nB;

return this;

};



private:

int m_nB;

};




main()

{

CSub* pMyInstance = new CSub();

pMyInstance->SetParameterB(1)->SetParameterA(2);

delete pMyInstance;

}


The problem is that if I call SetParameterA() before SetParameterB()
I get a compiler error. I change the third line from the end to:

pMyInstance->SetParameterA(2)->SetParameterB(1);


When I compile the program I get the following error:

> g++ test.cc

test.cc: In function 'int main()':

test.cc:37: error: 'class CBase' has no member named 'SetParameterB'


This is because SetParameterA() returns a pointer to CBase and CBase
does not have a method SetParameterB().

So I'm trying to find a solution to this problem.

One thing that I could do is make the method SetParameterA()
polymorphic, override it in CSub, call the SetParameterA() method in
base class and then type cast the return pointer. The problem with
this approach is that I would have to write wrappers for all methods
in the base class CBase in the derived class CSub.

Would you by any chance have a more elegant solution to this problem?

Thanks for you help,
Kjell
 
Reply With Quote
 
 
 
 
Al Balmer
Guest
Posts: n/a
 
      01-16-2008
On Tue, 15 Jan 2008 16:13:47 -0800 (PST), "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

>I wonder if someone may have a solution to a small problem I'm
>experiencing with a C++ program?


You're more likely to get useful answers in comp.lang.c++.

--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-16-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> I wonder if someone may have a solution to a small problem I'm
> experiencing with a C++ program?


You want comp.lang.c++ not comp.lang.c.

--
Ben.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> I wonder if someone may have a solution to a small problem I'm
> experiencing with a C++ program?


Someone in <news:comp.lang.c++> might. The _first_ thing to learn is
that C and C++ are different languages.
 
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
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Attribute Setting Methods kjell@cablescan.com C++ 3 01-16-2008 10:24 AM
trap setting attribute when the attribute is dict Python 6 09-04-2007 02:51 AM
picking value of one attribute based on a child elements attribute? XPath? vjethava@gmail.com XML 2 03-06-2006 05:19 AM
XSLT: Making attribute to parent attribute Bostonasian XML 1 09-18-2005 07:30 AM



Advertisments