2 Argument Conversion Function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: 2 Argument Conversion Function

Hybrid View

  1. #1
    Join Date
    Jul 2007
    Posts
    233

    Exclamation 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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    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?
    Code:
    Demo d=100,200;
    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.

  3. #3
    Join Date
    Jul 2007
    Posts
    233

    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)

  4. #4
    Join Date
    Apr 1999
    Posts
    27,446

    Re: 2 Argument Conversion Function

    Quote Originally Posted by Rajesh1978 View Post
    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:
    Code:
    Demo d(100, 200);
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center