@lucifer: thanks !

On Tue, Jan 3, 2012 at 9:02 AM, Lucifer <[email protected]> wrote:

> @Arun...
>
> Basically what u have asked boils down to 2 questions...
>
> 1. First, this sort of assignment requires single parameterized
> constructor ?
>
> Yes ( and No as well in special cases.).
> But its not a mandate that the class defines...
>
> There is something called as implicit and explicit construction...
>
> Class X
> {
>  public:
>   X(){} // default constructor...
>   X(int a) // single param constr
>   X( int a, double b) // const with 2 parameters...
> };
>
> Now there are 2 ways of creating an object of class X which takes only
> one parameter..
>
> a)  X obj(10); // this is the most common way of doing it...
>          // also here we are explicitly asking the compiler to use a
> particular constr whose prototype should match X(int );
>
> b) X obj = 10; // this is more uncommon way of doing it..
>  // here , an implicit conversion happens and it goes as follows:
> /*
>   i)  X temp = X(10); // creating temp object..
>   ii) X obj = temp; // overloaded '=' operator called
>   iii)  temp.~temp(); // destruct temp...
>
> Though, theoritically this is how it should create obj..
> But, due to compiler optimization a temporary object is not created
> and destroyed...
>
> But, to verify that there is a difference b/w implicit and explicit
> creation, u can look at generated assembly code and see that a
> reduntant copy operation is present in the assembly level code..
> This redundant copy code is to mimic the usage of default '='
> overloaded operator...i.e something similar to * mov bx, bx *
>
> */
> Given the above explanation we can say that we want to implicitly
> create an object which takes 2 parameters... Now, given the fact that
> '=' is a binary operator, there is no way we can use more than 2
> parameters in the expression..
>
> Say, i want to create:
>  X obj(10, 2.0), implicitly... then ' X obj = 10 20 ' needs to be a
> valid expression.. Basically for this is happen there needs to be a
> way where the rhs can take more than 2 expression values.. which is
> not possible...
>
> Hence, implicit creation of objects only work with single
> parameterized constructors...
>
> Now, you must have seen that i have used the following statement
> above:
> *But its not a mandate that the class defines...* - the above
> explanation should justify this statement..
>
> *Yes ( and No as well in special cases.* - *Yes* is obvious as per the
> above explanation...
> Now, to show what *No* meant, say class X has the following
> constructor instead of the single parameterized constructor...
> ///
> X( int a, char *b = NULL, bool b = true);
> ///
> Now, by looking at it we can say that its not a single parameterized
> constr..hence it can't be used in implicit construction..
> But thats not true as the constructor as got default parameters except
> the first one which is int type...
>
> Hence, given the above constructor.. X obj = 10, is still valid...
>
>
> 2. Does this mean that if a constructor function was not specified
> (unlike here where we have a parameterized constructor) this would not
> work ?
>
> Yes, should be self explanatory..
>
>
> ----------------------------------------------------------------------------------------------------
>
> Though its obvious but a conversion only works when RHS of '='
> operator can itself be assigned to the formal argument parameter of a
> constructor...
> Basically what i mean from the above statement is,
> apart from the basic types... it can also work for related(different
> not same) class types... say, the formal parameter of constrcutor has
> a base class type and the RHS of '=' has a derieved class type...
>
>
>
> On Jan 3, 7:52 pm, Arun Vishwanathan <[email protected]> wrote:
> > @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.
>
>


-- 
 "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.

Reply via email to