473,770 Members | 2,065 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
14 1756
Ron Natalie schrieb:
That's not a constructor call. [...] 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.


That's why I wrote indirectly.
Jul 23 '05 #11
Howard wrote:
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!
Or it says that the return type is specified and unchangable. I don't
know that with that text.
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.)


Right.
Jul 23 '05 #12
Aslan Kral wrote:

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

"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?


Creation of a temporary object

And calling the constructor as a result. Does it matter to call it John or
Mary?


Yes, it does.

The C++ programmer cannot directly call a constructor (for good reasons).
The constructor is no ordinary function, it doesn't participate in function
name lookup. Thus it is impossible to call it.

But one can create an object in various ways. Part of that object construction
is that the constructor gets called. That's a subtle different thing and answers
a question that comes up frequently: "How can I call a constructor from inside
another constructor? Why doesn't it work the way I do it?"

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

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

"Karl Heinz Buchegger" <kb******@gasca d.at>, haber iletisinde sunlari
yazdi:42******* ********@gascad .at...
Aslan Kral wrote:
>
> "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?

Creation of a temporary object And calling the constructor as a result. Does it matter to call it John or Mary?


Yes, it does.

The C++ programmer cannot directly call a constructor (for good reasons).
The constructor is no ordinary function, it doesn't participate in

function name lookup. Thus it is impossible to call it.

I didn't say you can call a constructor directly. I should have said
"indirectly " which I didn't and you assumed I meant "directly".

My point was to show that entire program could just be one line as an
example of "Concatenat ing Calls" starting with object creation at the
beginning (so constructor called *indirectly*).

class X
{
int i;
public:
X(int i_){ i=i_; }
X& M1(int i) { ++i; return *this; }
int M2(int i) { return --i; }
};

int main()
{
return X(1).M1(2).M2(3 );
}

But one can create an object in various ways. Part of that object construction is that the constructor gets called. That's a subtle different thing and answers a question that comes up frequently: "How can I call a constructor from inside another constructor? Why doesn't it work the way I do it?"

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

Jul 23 '05 #14
Aslan Kral wrote:


I didn't say you can call a constructor directly. I should have said
"indirectly " which I didn't and you assumed I meant "directly".
I think all of the regulars understood what you ment.
Nevertheless, all I can say is: Watch your language!
We are mostly dealing with newbies, so using a proper
nomenclature is important.
My point was to show that entire program could just be one line as an
example of "Concatenat ing Calls" starting with object creation at the
beginning (so constructor called *indirectly*).


So why didn't you say so in the beginning:

You can even begin with creating a temporary object like so:
SetMe().setX( 20 ).setY( 30 );

and nobody would have objected to anything.

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

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
3073
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
3683
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
2784
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
9425
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,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9869
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
6676
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
5312
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...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.