-
November 26th, 2012, 05:35 AM
#1
2 Argument Conversion Function
i have used single argument conversion function which gets called in below scenario.
Code:
#include<iostream>
using namespace std;
class Demo
{
public:
int a,b;
Demo(int i,int j=0):a(i),b(j)
{
}
void dis()
{
cout<<a <<"::"<<b<<endl;
}
};
int main()
{
Demo d=100;
d.dis();
return 0;
}
the output is 100::0
when i changed few lines in main so that the two argument constructor gets called
Code:
int main()
{
Demo d=100,200;
d.dis();
return 0;
}
It is giving error "test3.cpp: In function `int main()':test3.cpp:18: syntax error before numeric constant"
But it should work as Demo d=100; works
-
November 26th, 2012, 05:41 AM
#2
Re: 2 Argument Conversion Function
OK, I'll ask a simple question:
What book, tutorial, etc. shows you that this is the way to construct an object with two arguments?
What does the book you're using now say about constructing an object with more than 1 argument?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 26th, 2012 at 05:45 AM.
-
November 26th, 2012, 05:47 AM
#3
Re: 2 Argument Conversion Function
What book, tutorial, etc. shows you that this is the way to construct an object with two arguments?
This i did not get from any book or tutorial;
I thought logically it should work.
In the first case when Demo d=100 ( constructor with one argument is called)
It is same as Demo d(100) ->the 1 argument constructor is called
likewise Demo d=100,200 should be like Demo d(100,200) and call the 2 argument constructor (it what i thought)
-
November 26th, 2012, 05:53 AM
#4
Re: 2 Argument Conversion Function
Originally Posted by Rajesh1978
This i did not get from any book or tutorial;
Sorry, but you cannot learn C++ this way. You are missing the basic fundamentals of C++ if you go on guessing what the language syntax should look like. You must use structured material, whether it is using good books, good tutorials, etc. Otherwise, you'll just throw stuff on the wall, hoping something sticks.
I thought logically it should work.
No, it doesn't work this way. Any book will show you right away how to construct objects.
In the first case when Demo d=100 ( constructor with one argument is called)
It is same as Demo d(100) ->the 1 argument constructor is called
likewise Demo d=100,200 should be like Demo d(100,200) and call the 2 argument constructor (it what i thought)
Throw all of this out the window, it doesn't work. There is something called the comma operator that makes your version uncompilable.
The way you construct objects is very basic and straightforward:
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 26th, 2012 at 05:59 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|