473,761 Members | 9,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Concatenating Calls

i am fresher to C++ programming, and I just
want to learn Concatenating Calls, I have
written a program,
class SetMe {
public:
void setX(int x) {_x = x;}
void setY(int y) {_y = y;}
void doubleMe()
{
_x *= 2;
_y *= 2;
}
private:
int _x;
int _y;
};
int main(){
SetMe lower;
((lower.setX(20 )).setY(30)).do ubleMe();
}
While compiling on gcc version 3.2.3 on Redhat Linux
it gives following errors,
point.c: In function `int main()':
point.c:17: request for member `setY' in
`(&lower)->SetMe::setX(in t)(20)', which is of non-aggregate type
`void'
Would somebody explain how to remove this error.
I want to retain the style of concatenating calls

thanks
divya

Jul 23 '05 #1
14 1754
foodic wrote:
i am fresher to C++ programming, and I just
want to learn Concatenating Calls, I have
written a program,
class SetMe {
public:
void setX(int x) {_x = x;}
setX returns void !
void setY(int y) {_y = y;}
So does setY.
void doubleMe()
{
_x *= 2;
_y *= 2;
}
private:
int _x;
int _y;
};
int main(){
SetMe lower;
((lower.setX(20 )).setY(30)).do ubleMe();
Here you use the return value from setX (which is void) and call setY.
You probably want to do this.

SetMe & setX(int x) {_x = x; return *this;}
SetMe & setY(int y) {_y = y; return *this;}
SetMe * doubleMe() { .... return *this;}
}
While compiling on gcc version 3.2.3 on Redhat Linux
it gives following errors,
point.c: In function `int main()':
point.c:17: request for member `setY' in
`(&lower)->SetMe::setX(in t)(20)', which is of non-aggregate type
`void'
Would somebody explain how to remove this error.
I want to retain the style of concatenating calls


Return the object by reference.
Jul 23 '05 #2
foodic wrote:

i am fresher to C++ programming, and I just
want to learn Concatenating Calls, I have
written a program,

class SetMe {
public:
void setX(int x) {_x = x;}
void setY(int y) {_y = y;}
void doubleMe()
{
_x *= 2;
_y *= 2;
}
private:
int _x;
int _y;
};
int main(){
SetMe lower;
((lower.setX(20 )).setY(30)).do ubleMe();
}
While compiling on gcc version 3.2.3 on Redhat Linux
it gives following errors,
point.c: In function `int main()':
point.c:17: request for member `setY' in
`(&lower)->SetMe::setX(in t)(20)', which is of non-aggregate type
`void'
Would somebody explain how to remove this error.
I want to retain the style of concatenating calls


The keypoint in this is, that each function has to return something
that the next call can act on.

If you want to do

SetMe lower;

lower.setX( 20 ).setY(30);

then the part

lower.setX( 20 )

must evaluate to something that is usable as object such
that setY can work on. Best you start with

SetMe lower;

SetMe someObj;
someObj.setY( 30 );

now replace someObj with lower.setX( 20 )

lower.setX( 20 ).setY( 20 );

from this it is clear that the expression "lower.SetX ( 20 )" must
have the same type as someObj before. Thus SetX must return a SetMe
object. (For obvious reasons I do the same modification on SetX,
SetY and doubleMe simultanously)

(Note: the following is not quite what you want, but close.
I come back to it in a minute)

class SetMe
{
public:
SetMe setX(int x) { _x = x; return *this; }
SetMe setY(int y) { _y = y; return *this; }

SetMe doubleMe()
{
_x *= 2;
_y *= 2;

return *this;
}

private:
int _x;
int _y;
};

Now every function returns a SetMe object. Thus when
lower.SetX( 20 )
is evaluated, its result is a SetMe object, which can be
used for the next call:

lower.setX( 20 ).doubleMe().se tY( 30 );

But wait: What is the exact return type of each function?
It is 'SetMe'. That means that for every return a copy
of the object that was worked on is returned. Thus in the
above the object for which doubleMe is called, is *not*
lower, but is an object which is an exact copy of lower right
after the call to SetX has finished. If this is not what you
want, then the return type needs some slight modification.
What we want is not a copy of the object, but the object itself.
We do this by returning a reference to the object:

class SetMe
{
public:
SetMe& setX(int x) { _x = x; return *this; }
SetMe& setY(int y) { _y = y; return *this; }

SetMe& doubleMe()
{
_x *= 2;
_y *= 2;

return *this;
}

private:
int _x;
int _y;
};

Now the returned object of setX is the very same object that
was used for the call to setX ( 'this' is the C++ way to say
'I' or 'me'. The only thing is that 'this' is a pointer type.
So to say 'return me' you have to dereference the pointer, hence
return *this; )

In

lower.setX( 20 ).setY( 30 );

the call to setX now returns a reference to the object it was
called with, which was 'lower'. Thus setY will again work on 'lower'
and do its work.

That's the whole secret.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #3

"Karl Heinz Buchegger" <kb******@gasca d.at>, haber iletisinde sunlari
yazdi:42******* ********@gascad .at...

<snip>

lower.setX( 20 ).setY( 30 );
You can even begin with the class contructor like this:

SetMe().setX( 20 ).setY( 30 );
// if you don't need "lower" after this line of course,
// say passing it finally into a function as a parameter.
the call to setX now returns a reference to the object it was
called with, which was 'lower'. Thus setY will again work on 'lower'
and do its work.

That's the whole secret.

--
Karl Heinz Buchegger
kb******@gascad .at

Jul 23 '05 #4
Aslan Kral wrote:
You can even begin with the class contructor like this:

SetMe().setX( 20 ).setY( 30 );


That's not a constructor. You can't call constructors nor do
they return a value.

Jul 23 '05 #5

"Ron Natalie" <ro*@sensor.com >, haber iletisinde sunlari
yazdi:42******* *************** @news.newshosti ng.com...
Aslan Kral wrote:
You can even begin with the class contructor like this:

SetMe().setX( 20 ).setY( 30 );


That's not a constructor. You can't call constructors nor do
they return a value.

What do you call it then? And how does it work on my pc? (See beloew)

class SetMe {
public:
SetMe()
{
printf("SetMe constructor\n") ;
}
SetMe& setX(int x) {_x = x; return *this; }
SetMe& setY(int y) {_y = y; return *this; }
SetMe& doubleMe()
{
_x *= 2;
_y *= 2;
return *this;
}
private:
int _x;
int _y;
};

int main()
{
printf("Before -- SetMe constructor\n") ;
SetMe().setX(1) .setY(2);
printf("After -- SetMe constructor\n") ;
return 0;
}

Output:
Before -- SetMe constructor
SetMe constructor
After -- SetMe constructor
Jul 23 '05 #6
Ron Natalie wrote:
That's not a constructor. You can't call constructors
I can call constructors, although indirectly. Look at this:

---------->8---------->8----------
class Foo
{
Foo(int) {}
};

int main()
{
Foo(10); // constructor call
}
---------->8---------->8----------
nor do they return a value.


ISO-IEC 14882-1998 §12.1.12:

"No return type (not even void) shall be specified for a constructor. A
return statement in the body of a constructor shall not specify a return
value. The address of a constructor shall not be taken."

There is not written that a constructor doesn't have a return type.
Please correct me if it's written at another place.

Jul 23 '05 #7

"Michael Etscheid" <th***********@ gmail.com> wrote in message
news:d0******** *****@news.t-online.com...
Ron Natalie wrote:
That's not a constructor. You can't call constructors


I can call constructors, although indirectly. Look at this:

---------->8---------->8----------
class Foo
{
Foo(int) {}
};

int main()
{
Foo(10); // constructor call
}
---------->8---------->8----------
> nor do they return a value.


ISO-IEC 14882-1998 '12.1.12:

"No return type (not even void) shall be specified for a constructor. A
return statement in the body of a constructor shall not specify a return
value. The address of a constructor shall not be taken."

There is not written that a constructor doesn't have a return type. Please
correct me if it's written at another place.


It looks like it says there is no return type pretty clearly, at least to
me. It says "No return type..shall be specified". That means that it does
not have a return type. How can you have a return type if you don't specify
what the type _is_? It also says it "shall not specify a return value".
That means that nothing _can_ be returned! In other words, if you have a
return statement in the constructor, it hsa to stand alone, as in "return;",
instead of specifying a return value as in "return 0;". So, if a
constructor does not specify a return type, and does not return a value, how
can you say it "has" a return type? I'd like to see an example of a
constructor that _does_ have a return type!

By the way, your example does not actually "call" the constructor. When you
write the statement "Foo(10);", you're creating an unnamed temporary
variable. The creation of that temporary does involve calling that
constructor, but technically you're not doing so yourself. (Which, I
suppose, is why you stated "although indirectly" above.)

-Howard

Jul 23 '05 #8
Michael Etscheid wrote:
Foo(10); // constructor call
correct me if it's written at another place.


That's not a constructor call. Constructors don't have names,
they don't participate in name look up. The syntax you have above
is actually an "explicit conversion (functional notation)" according
to the language syntax.

What it does is convert 10 to type Foo (creating a temporary in
the process). The creation of this temporary involves allocation
of sizeof (FOO) bytes of memory and then initializing it by calling
the Foo(int) constructor.

It's impossible for user code to call constructors, they can only
create objects in ways that the implmentation invokes the constructors
for them.
Jul 23 '05 #9
Ron Natalie wrote:
Michael Etscheid wrote: ....

It's impossible for user code to call constructors, they can only
create objects in ways that the implmentation invokes the constructors
for them.


Except for placement new. This is essentially a constructor call (not
in the strict C++ sense).
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2051
by: dont bother | last post by:
Hey, I have these attributes: index which is a numerical value value vector which is a numerical float value and I want to concatenate like this:
6
2286
by: nwheavyw8 | last post by:
I am currently trying to write a simple PHP script that will split an uploading file up into 500kb "chunks", then read and concatenate them back together when accessed for download. I can't seem to be able to find a way to split the file purely in PHP while it is in the middle of uploading using the move_uploaded_file function. I am trying to get it to store the file like so: MySong.mp3 3mb gets uploaded to my server in this...
16
3071
by: Dixie | last post by:
I have a problem using Dev Ashish's excellent module to concatenate the results of a field from several records into one record. I am using the code to concatenate certain awards onto a certificate at the end of the year. I have the code working fine, except for the fact that when I want to restrict the entries to awards between certain dates, even though I can use the restriction in the query that shows the actual records, when the...
4
2349
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see them in the debugger). After encoding the string (the sameone which complete value is not visible from the debugger) all the values can be seen but they are spaced by big amounts of zeros and use more that the 2048 bytes allocated. It is like if...
1
3682
by: ebobnar | last post by:
I need to call the function LoadImage which take a LPCTSTR argument to specify the path to the image to load. However, I need to create this path dynamically from user input by concatenating strings together. I'm using visual c++.net 2003. I've tried using the String class to put the image path together and then cast the String to a LPCTSTR, but that cast is forbidden. Since I'm using visual c++.net should I not be using functions like...
1
1221
by: Ani | last post by:
I have a questionaire page , which basically has questions with multiple choice answers. I need to accomplish paging on this and there are few questions that are gender specific. According the person who has logged in, if the person is male, i need to generate only those questions specific to males . I have a function that returns the contents of the questionaire in the form of a datatable. I have successfully displayed all the questions...
0
4825
by: Big D | last post by:
Hey there. I'd like to concatenate multiple wav files together. I've got an almost completely working system for it, actually... I'm using AudioSystem.write and my own subclass of AudioInputStream to do it. Basically, my AudioInputStream contains a List of AudioInputStreams, and for each one added, I add to its frameLength so it reports as being the right length. All my source files are the same type, so that is a safe assumption to...
7
2783
by: Mary | last post by:
I have a student who has a hyphenated first name. If I concatenate the name like this: StudentName:( & ", " & ), it works as expected. If, however, I try to get the first name first by concatenating like this: StudentName: ( & " " & ), only the first name appears. I sure would appreciate any help! Mary
5
3739
by: JRNewKid | last post by:
I want to concatenate two fields on a report. They are two text fields, wrkDescription is 10 characters long and wrkTextDescription is 255. I have them concatenated in the report but I'm only getting one line of the long field. Before concatenation i was getting the whole thing. This is what I want it to look like: Clerical: Typed reports for five people in the field, made phone calls for the new hire, arranged a cubicle for the temp...
0
9376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.