-3

I want to be able to be able to do something like this:

myDataType Data1; 
myDataType Data2;

Data1 = "Something";
Data2 = Data1;

Setting Data1 is easy because I overload the = operator. What I'd like to be able to do is type line 2 as is. Without a default property, I would have to type it as:

Data2 = Data1.theDesiredProperty;

It seems to me that it should be possible to do this, because the string class works in this way. If you replace "myDataType" with "string" (and inlcude the string header file) you don't have to refer to some default property of string.

Thanks.

C_Rod
  • 193
  • 10
  • "Setting Data1 is easy because I overload the = operator". That operator isn't used there. – juanchopanza Apr 25 '15 at 07:54
  • This data type has four overloaded constructors already. But to get the value out of an instance of this data type, I currently have to type: myDataType.value. I'm working in a group environment where it would be useful to not have to do this. – C_Rod Apr 25 '15 at 07:55
  • http://en.cppreference.com/w/cpp/language/as_operator – Karoly Horvath Apr 25 '15 at 07:58
  • juan, that operator does exist here. jrok, the overloaded operator isn't my issue, we have about 7 of them for different data types defined for about 10 different operators. The issue is how to extract a default property from the right hand argument. That is, I currently would have to type: Data2 = Data1.someproperty; – C_Rod Apr 25 '15 at 08:03
  • Karoly, this might be what I need. Thanks. – C_Rod Apr 25 '15 at 08:06
  • well, they just responded to your original code... – Karoly Horvath Apr 25 '15 at 08:07
  • Yeah, I realized it would be slightly different assigning it as I declared it. This handles the left hand side which I can already do. I'm trying to find a fancy way to have a default property for the right hand side. – C_Rod Apr 25 '15 at 08:10
  • OK I figured it out. If you take an extra step to manually set the property for the RHS in the header file, you don't have to do it in the main program. Thanks. – C_Rod Apr 25 '15 at 08:26

2 Answers2

1

I think the most useful thing here would be to see an example of how to do this:

class myDataType {
    public:
        myDataType(const char *message = ""): message_(message) {}

        const char * message() const {
            return message_;
        }

    private:
        const char *message_;
};


int main() {
    myDataType Data1;
    myDataType Data2;

    Data1 = "Something";
    Data2 = Data1;

    myDataType Data3 = "Something";
    myDataType Data4 = Data1;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • what does the explicit keyword do in this case? Does it not prevent implicit conversion? – C_Rod Apr 25 '15 at 18:50
  • Thanks for taking the type to help me figure this out. :-) – C_Rod Apr 25 '15 at 18:51
  • Oh, whoa, this edit is a new construct. Can you explain a bit what is going on in the message part? (line 3) – C_Rod Apr 25 '15 at 18:54
  • 1
    This seems to help: http://stackoverflow.com/questions/2785612/c-what-does-the-colon-after-a-constructor-mean – C_Rod Apr 25 '15 at 18:58
0

OK, so the answer is to overload the operators globally and use accesser functions to read the desired property from the object. All of my experience with operator overloading has been to put them in the class and in that position they only handle the object if it is on the left hand side. In order to handle the object on the right hand side in the way I desire, the operator overload should be outside the class definition and use the keyword friend. I'm not sure why it was so hard to get that missing piece of information, but here it is if anyone else is looking.

friend bool operator==(const std::string& lhs, const myDataType& rhs);
C_Rod
  • 193
  • 10