开发者

Constructor is being invoked twice

In code:

//file main.cpp
LINT a = "12";
LINT b = 3;
a = "3";//WHY THIS LINE INVOKES CTOR?


std::string t = "1";
//LINT a = t;//Err NO SUITABLE CONV FROM STRING TO LINT. Shouldn't ctor do it?

//file LINT.h
#pragma once
#include "LINT_rep.h"
class LINT
{
private:
    typedef LINT_rep value_type;
    const value_type* my_data_;
    template<class T>
    void init_(const T&);
public:
    LINT(const char* = 0);
    LINT(const std::string&);
    LINT(const LINT&);
    LINT(const long_l开发者_如何学Goong&);
    LINT& operator=(const LINT&);
    virtual ~LINT(void);

    LINT operator+()const;               //DONE
    LINT operator+(const LINT&)const;//DONE
    LINT operator-()const;               //DONE
    LINT operator-(const LINT&)const;//DONE
    LINT operator*(const LINT&)const;//DONE
    LINT operator/(const LINT&)const;///WAITS FOR APPROVAL

    LINT& operator+=(const LINT&);//DONE
    LINT& operator-=(const LINT&);//DONE
    LINT& operator*=(const LINT&);//DONE
    LINT operator/=(const LINT&);///WAITS FOR APPROVAL
};

in line number 3 instead of assignment optor ctor is invoked. Why? I'm willing to uppload entire solution on some server otherwise it's hard to put everything in here. I can also upload video file. Another thing is that when I implement this assignment optor I'm getting an error that this optor is already in obj file? What's going on?


You don't have an = operator which takes a RHS of std::string (or char*), so, the literal '3' is being constructed to a LINT, and then assigned using your = operator.

EDIT: As for the 2nd question in your code, you need to call c_str() on the std::string to get the char* buffer of the string, then the same thing will happen as with your literal 3.


Your assignment operator takes a LINT object as a parameter, but when you say:

a = "3";

you are handing the assignment op a string literal, not a LINT object. The compiler needs to create a LINT object that the assignment op can use, so it calls the constructor that takes a const char * as a parameter to do that.


The following constructor:

LINT(const char* = 0);

will be called for literal assignment "3" because it is acting as an implicit constructor call. If you wish to avoid that, prefix the constructor with the 'explicit' qualifier.

Also add an assignment operator for any types you wish to assign without implicit construction.


Because there is no assignment operator specified which could be used. You probably need something like: LINT& operator=(const char*);


The constructor

LINT(const char* = 0);

acts as conversion constructor for assignments to char* (e.g. LINT a = "3"). Your =()-operator only is called if you assign a LINT object to another LINT object.

LINT a;
LINT& b = a
LINT& c = LINT("4");

The expressions above will call your =()-operator.


There is LINT& operator=(const LINT&); There is no LINT& operator=(const char *); But there are many implicit ctors. Therefore implict ctor is invoked.


The only assignment operator your LINT class defines is the copy assignment operator, LINT& operator=(const LINT&). Thus, on line 3, when you attempt to assign a static C-style string (const char[]) to a LINT object, the compiler recognizes that there is no matching assignment operator.

However, the compiler is allowed to use one user-defined type conversion to make the call work. It selects the LINT(const char*) constructor to turn the const char[] into a LINT object, then invokes the copy assignment operator on the temporary LINT object it creates for the right-hand side.

You can avoid this temporary by providing a LINT& operator=(const char*) assignment operator to augment your copy assignment operator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜