@lucifer: ok so you are saying that the constructor implicitly creates a temporary 'string' object to hold this char string which is then assigned to s2. Does this mean that if a constructor function was not specified (unlike here where we have a parameterized constructor) this would not work or does this sort of assingment require a single paramterized constructor?
On Tue, Jan 3, 2012 at 12:31 AM, Lucifer <[email protected]> wrote: > @above.. > > This is what i assume is happening ( apart from inherent compiler > optimization is any)...Let me know if i m wrong.. > > when s2=name; is done it should call the overloaded equal to > operator.. > > But, 'name' is not a string object, its basically a char pointer to a > const string "test".. > Now, for simplicity lets assume that name is char array.. > > Now, given a binary operator, for the operation to take place both the > operands ideally should be of the same type... > > For ex: > int a; > a = 10.0; > Here, 10.0 is double and a is int, for the assignment to work first > 10.0 will be converted to int data type and then assigned to a.. > > In case, the right hand side of a = operator cannot be converted to > the left hand side type, then ideally an incompatible assignment shall > be thrown.. > > Going back to the above example... conversion of 10.0 to 10 is > basically performed as part of implicit conversion or type propagation > as part of basic data types (supported by the compiler)... > > Now class is a custom data type and hence, we don't expect the > compiler to randomly convert from any data type to the class type for > the '=' operator to work.. > Then how is it done.. > Basically constructors of a class act as implicit type converters as > well... > Hence, for statement similar to s2 = name; > If 'name' is not of the type of s2 i.e.'string' type then it will try > to look for implicit conversions.. > Now, a constructor of a class acts as an implicit converter as well.. > and a 'string' class has a constructor 'string(char *)', it will use > 'string(char*)' constructor to construct a temporary intermediate > string object which will hold the value 'test' and then assign to > s2... > Once, assignment operation is over, the temporary string object > containing the value 'test' will be destroyed.. > > On Jan 3, 12:05 pm, Arun Vishwanathan <[email protected]> wrote: > > I just have a basic doubt..does the string s1,s2 statement call any > default > > constructor?or is it that it is not performed since parameterised > > constructor is present? > > > > On Wed, Sep 21, 2011 at 1:31 AM, vijay singh <[email protected] > >wrote: > > > > > > > > > > > > > > > > > > > > > It is because of the presence of the single parameterised constructor > in > > > the class definition. > > > So, if we are writing the following statement... > > > string s1; > > > s1="test"; > > > > > It'll call the single parameterised constructor. > > > > > But this only true in the case of single value assignment as in the > above > > > statement.. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Algorithm Geeks" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]. > > > For more options, visit this group at > > >http://groups.google.com/group/algogeeks?hl=en. > > > > -- > > "People often say that motivation doesn't last. Well, neither does > bathing > > - that's why we recommend it daily." > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- "People often say that motivation doesn't last. Well, neither does bathing - that's why we recommend it daily." -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
