"Default" class member
-
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
-
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
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 yourstruct
.
"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
-
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
Not generically no. What you can do is write a custom cast operator so that when the Compiler attempts to cast your
myclass
to achar*
to match the parameters ofstrncpy
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.
-
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
Sometimes called a cast operator[^] You also want to read what Scott Meyers has to say about it in Effective C++[^]
-
Not generically no. What you can do is write a custom cast operator so that when the Compiler attempts to cast your
myclass
to achar*
to match the parameters ofstrncpy
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.
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
-
Sometimes called a cast operator[^] You also want to read what Scott Meyers has to say about it in Effective C++[^]
-
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
-
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
-
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