overloaded operator=

I have used class overloads of operators many times before, including overloading operator=, however for some reason it is not building in this particular instance where I'm trying to convert from one object (struct CAirportNDB) to a class (class CAirport). I cannot see where the problem is myself, so any input as to what I'm doing wrong here (or suggestions as to how to do it better, noting that FindAirport() is a function that I don't really have control over) would be greatly appreciated!

header file ndb_types.h:

1
2
3
4
5
6
7
namespace ndbm
{
    struct SAirportNDB
    {
        // attributes here
    };
}

header file Airport.h:

1
2
3
4
5
6
7
8
9
10
11
12
#include "ndb_types.h"
using ndbm::SAirportNDB;

namespace fpln
{
    class CAirport
    {
    public:
        // attributes here
        const CAirport& operator=(const SAirportNDB& rhs);
    };      
}

source file Airport.cpp:

1
2
3
4
5
6
7
8
9
10
#include "Airport.h"

namespace fpln
{
    const CAirport& CAirport::operator=(const SAirportNDB& rhs)
    {
        // conversion here
        return *this;
    }
}

source file implementation (say main.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include "Airport.h"

namespace fpln
{
    void any_function(void)
    {
        std::string ident("ABCD");
        const SAirportNDB* ndb_arpt = FindAirport(ident); // FindAirport() returns pointer to SAirportNDB type

        if (ndb_arpt)
            CAirport arpt = *ndb_arpt; // error C2440: 'initializing' : cannot convert from 'const ndbm::SAirportNDB' to 'fpln::CAirport'
    }
}
Last edited on
Your code in main.cpp is NOT using operator=, it is trying to use a constructor. In other words, what you have there is construction, not assignment.
It shouldn't return a const ref.
Only member functions (methods) have a this pointer, so you can't return this from a non-member function.
Ah hah, thanks for catching that webJose! I'm sure I could add a constructor for this to reduce an operation, but changing the code to the following fixes the problem (separates construction and equation):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include "Airport.h"

namespace fpln
{
    void any_function(void)
    {
        std::string ident("ABCD");
        const SAirportNDB* ndb_arpt = FindAirport(ident); // FindAirport() returns pointer to SAirportNDB type

        if (ndb_arpt)
        {
            CAirport arpt; 
            arpt = *ndb_arpt;
        }
    }
}

In regards to returning a const ref, it is being returned from a member function so I'm not entirely sure what you mean. Const ref is returned to allow operator= chaining.
I think traditionally it is just a ref, not a const ref that is returned from operator=.
I have seen that, but wouldn't that allow a left hand side variable in an equation chain to accidently screw up the right hand side (original) value? I know this is unlikely but I always make it a const just to be safe. You never know who is going to end up using your code!
No because operator='s right-hand side is traditionally const. True, someone might make a non-const version. But that is their loss, not yours, right? I mean, it is their program that will be damaged, not your library. But I guess const works just fine too.

And in any case, anyone really wanting to, could use const_cast to wave constness away.
I guess I might as well make it non-const just to go with standard convention, which of course if everyone would follow makes coding life much simpler. Thanks for pointing out my non-compliance with that, you are right if someone else goes against standard convention and messes it up that is their own problem!

Thanks again for all the help with this!!
Topic archived. No new replies allowed.