Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Constructor conversion.

Constructor conversion.

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WREY
    wrote on last edited by
    #1

    According to the rules of C++, a constructor for a class taking a single parameter, could be used to implicitly convert the value of the constructor's parameter type to a value of the constructor's class type. That means, I ought to be able to convert an "int" value, to a "Number" value. That is not happening. Can anybody see why? Thanks! ====================================

    class Acct;

    class Number
    {
    public:
    Number(int i=0) : n(i) {}
    Number(const Acct& ac) : acct(ac) {}

    void print(Number nbr) { cout<< " Nbr: " << nbr <

    Because it's an "int" I'm printing (as a result of the conversion), I should be able to use the regular standard I/O operators to output the result. The compiler doesn't believe so, and is complaining about that too.

    :confused:

    William

    Fortes in fide et opere!

    V A 2 Replies Last reply
    0
    • W WREY

      According to the rules of C++, a constructor for a class taking a single parameter, could be used to implicitly convert the value of the constructor's parameter type to a value of the constructor's class type. That means, I ought to be able to convert an "int" value, to a "Number" value. That is not happening. Can anybody see why? Thanks! ====================================

      class Acct;

      class Number
      {
      public:
      Number(int i=0) : n(i) {}
      Number(const Acct& ac) : acct(ac) {}

      void print(Number nbr) { cout<< " Nbr: " << nbr <

      Because it's an "int" I'm printing (as a result of the conversion), I should be able to use the regular standard I/O operators to output the result. The compiler doesn't believe so, and is complaining about that too.

      :confused:

      William

      Fortes in fide et opere!

      V Offline
      V Offline
      Vimal Earnest
      wrote on last edited by
      #2

      William, The conversion from int to Number is legitimate in C++, and my VC++ 6.0 compiles it fine. But the print function wont compile, since you are printing a 'Number' and not an int.The compiler has no idea about printing the Number type.To do this you need to overload the insertion operator "<<". ~ Vimal

      W 1 Reply Last reply
      0
      • W WREY

        According to the rules of C++, a constructor for a class taking a single parameter, could be used to implicitly convert the value of the constructor's parameter type to a value of the constructor's class type. That means, I ought to be able to convert an "int" value, to a "Number" value. That is not happening. Can anybody see why? Thanks! ====================================

        class Acct;

        class Number
        {
        public:
        Number(int i=0) : n(i) {}
        Number(const Acct& ac) : acct(ac) {}

        void print(Number nbr) { cout<< " Nbr: " << nbr <

        Because it's an "int" I'm printing (as a result of the conversion), I should be able to use the regular standard I/O operators to output the result. The compiler doesn't believe so, and is complaining about that too.

        :confused:

        William

        Fortes in fide et opere!

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Try changing the print to take a reference: void print (Number &nbr)

        1 Reply Last reply
        0
        • V Vimal Earnest

          William, The conversion from int to Number is legitimate in C++, and my VC++ 6.0 compiles it fine. But the print function wont compile, since you are printing a 'Number' and not an int.The compiler has no idea about printing the Number type.To do this you need to overload the insertion operator "<<". ~ Vimal

          W Offline
          W Offline
          WREY
          wrote on last edited by
          #4

          Thanks Vimal. You make a lot of sense and you are correct. However, I had done just what you recommended before posting the note, and everything did work for me. But when examined, overloading the stream insertion operator "<<" didn't looked like it bought me anything, because I could have obtained the same result if I had not used constructor conversion and had done things the regular way, using an "int" instead for my print() parameter. I figured as part of the ctor conversion technique, one of the benefits I would obtain, would be NOT having to overload the "<<" operator. Secondly (and this is the part that made me refused to give up), I thought somebody might know of a way (that I didn't) to make what I was trying to achieve, work. I thank you for your response because it tells me what I was dreaming of doing, will just not work. Thanks again! :cool: William Fortes in fide et opere!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups