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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. "Default" class member

"Default" class member

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelplearning
10 Posts 5 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.
  • D Offline
    D Offline
    DQNOK
    wrote on last edited by
    #1

    Sorry for the sophomoric nature of this question... I've looked thru an old Stroustrup book, and Googled it and didn't find an answer. I have a class with three data members. When used in a bare context (no operators surrounding it), I would like the class variable to return the first data member. I can't use the default copy constructor because that is already used for other purposes.

    struct myclass{
       char *str;
       size_t len;
       size_t ofst;
    };
    
    myclass a1;
    
    char strng[50];
    
    strncpy( strng, a1, 50 ); //error. should have been
                              //strncpy(strng, a1.str, 50);
    

    If str could be made the "default" member, the above line would work. Is this possible? David

    D L M 3 Replies Last reply
    0
    • D DQNOK

      Sorry for the sophomoric nature of this question... I've looked thru an old Stroustrup book, and Googled it and didn't find an answer. I have a class with three data members. When used in a bare context (no operators surrounding it), I would like the class variable to return the first data member. I can't use the default copy constructor because that is already used for other purposes.

      struct myclass{
         char *str;
         size_t len;
         size_t ofst;
      };
      
      myclass a1;
      
      char strng[50];
      
      strncpy( strng, a1, 50 ); //error. should have been
                                //strncpy(strng, a1.str, 50);
      

      If str could be made the "default" member, the above line would work. Is this possible? David

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      DQNOK wrote:

      strncpy( strng, a1, 50 ); //error. should have been //strncpy(strng, a1.str, 50);

      This might work if you had a const char* operator (strncpy()'s second argument type) in your struct.


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • D DQNOK

        Sorry for the sophomoric nature of this question... I've looked thru an old Stroustrup book, and Googled it and didn't find an answer. I have a class with three data members. When used in a bare context (no operators surrounding it), I would like the class variable to return the first data member. I can't use the default copy constructor because that is already used for other purposes.

        struct myclass{
           char *str;
           size_t len;
           size_t ofst;
        };
        
        myclass a1;
        
        char strng[50];
        
        strncpy( strng, a1, 50 ); //error. should have been
                                  //strncpy(strng, a1.str, 50);
        

        If str could be made the "default" member, the above line would work. Is this possible? David

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        Not generically no. What you can do is write a custom cast operator so that when the Compiler attempts to cast your myclass to a char* to match the parameters of strncpy str is used. Something like:- operator char*() { return str; } defined within the struct. This is probably not considered 'good' code but as you asked.:)

        Nothing is exactly what it seems but everything with seems can be unpicked.

        D 1 Reply Last reply
        0
        • D DQNOK

          Sorry for the sophomoric nature of this question... I've looked thru an old Stroustrup book, and Googled it and didn't find an answer. I have a class with three data members. When used in a bare context (no operators surrounding it), I would like the class variable to return the first data member. I can't use the default copy constructor because that is already used for other purposes.

          struct myclass{
             char *str;
             size_t len;
             size_t ofst;
          };
          
          myclass a1;
          
          char strng[50];
          
          strncpy( strng, a1, 50 ); //error. should have been
                                    //strncpy(strng, a1.str, 50);
          

          If str could be made the "default" member, the above line would work. Is this possible? David

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Sometimes called a cast operator[^] You also want to read what Scott Meyers has to say about it  in Effective C++[^]

          D 1 Reply Last reply
          0
          • M Matthew Faithfull

            Not generically no. What you can do is write a custom cast operator so that when the Compiler attempts to cast your myclass to a char* to match the parameters of strncpy str is used. Something like:- operator char*() { return str; } defined within the struct. This is probably not considered 'good' code but as you asked.:)

            Nothing is exactly what it seems but everything with seems can be unpicked.

            D Offline
            D Offline
            DQNOK
            wrote on last edited by
            #5

            I think you nailed it. In my original post I had started to ask if it was good form, but decided to wait to see if it was even possible. Another poster has directed me to a later Stroustrup book. I'll take a look to see what Mr. Stroustrup says. Thanks a lot. David

            D 1 Reply Last reply
            0
            • L led mike

              Sometimes called a cast operator[^] You also want to read what Scott Meyers has to say about it  in Effective C++[^]

              D Offline
              D Offline
              DQNOK
              wrote on last edited by
              #6

              Thanks. I had forgotten about user defined type conversions (C++, Stroustrup, 7.3.2) I don't have the Effective C++ book on hand, and the link doesn't divulge Scott's secrets. Stroustrup discusses some of the issues. Maybe that's enough... David

              L 1 Reply Last reply
              0
              • D DQNOK

                I think you nailed it. In my original post I had started to ask if it was good form, but decided to wait to see if it was even possible. Another poster has directed me to a later Stroustrup book. I'll take a look to see what Mr. Stroustrup says. Thanks a lot. David

                D Offline
                D Offline
                DQNOK
                wrote on last edited by
                #7

                Oops; it's a Scott Meyers book, not a Stroustrup book...

                1 Reply Last reply
                0
                • D DQNOK

                  Thanks. I had forgotten about user defined type conversions (C++, Stroustrup, 7.3.2) I don't have the Effective C++ book on hand, and the link doesn't divulge Scott's secrets. Stroustrup discusses some of the issues. Maybe that's enough... David

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  IMHO Meyers work is a "must read".

                  D J 2 Replies Last reply
                  0
                  • L led mike

                    IMHO Meyers work is a "must read".

                    D Offline
                    D Offline
                    DQNOK
                    wrote on last edited by
                    #9

                    I just now ordered it thru Amazon.

                    1 Reply Last reply
                    0
                    • L led mike

                      IMHO Meyers work is a "must read".

                      J Offline
                      J Offline
                      jhwurmbach
                      wrote on last edited by
                      #10

                      led mike wrote:

                      IMHO Meyers work is a "must read".

                      Definitely!


                      Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                      George Orwell, "Keep the Aspidistra Flying", Opening words

                      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