Jump to content
  • Advertisement
Sign in to follow this  
nlbs

fnc("something") = Obj

This topic is 3638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My function fnc returns a X* Type object. So can I use fnc("something") = Obj if Obj is of type X* or XDerived* I've already tried and compiler fired error saying.
error: lvalue required as left operand of assignment
On the line where I do fnc("something") = Obj;

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by nlbs
My function fnc returns a X* Type object.
So can I use fnc("something") = Obj
No.

Unless your function returns a reference to a pointer, you can't assign to its return value like this. So, for example:

X* &get()
{
static X* internal;
return internal;
}

get() = new X();

Share this post


Link to post
Share on other sites
Thanks fro that Information.

Hmm In method signature I can use.

Js::JType* &operator()(const string& key);

But what would I put in cpp

Js::JType* &Js::Object::operator()(const string& key){

fires Compilation errors.

Share this post


Link to post
Share on other sites
jobject.cpp:37: error: invalid initialization of reference of type 'Js::JType*&' from expression of type 'Js::JType* const'


for the following line

Js::JType* &Js::Object::operator()(const string& key){


EDIT

Sorry the error is for a different reason
As I've already posted I am not deleting it.

I think &Js::Object::operator()(const string& key) doesn't causes the problem
overlooked the error message

Share this post


Link to post
Share on other sites
In that function I've return it->second.
it is of type Iterator (non-const Iterator) and on that line Its firing that above error.

jobject.cpp:37: error: invalid initialization of reference of type 'Js::JType*&' from expression of type 'Js::JType* const'


Now from where it getting the const I am not able to understand.

it is an iterator to a stl map.
std::map<std::string, Js::JType*>

EDIT

However Casting it directly works
return (Js::JType* &)(it->second)

[Edited by - nlbs on November 5, 2008 9:29:52 AM]

Share this post


Link to post
Share on other sites
You need to be extremely careful when casting, especially a C-style cast.

This compiles:

#include <map>
#include <string>
#include <stdexcept>

struct Type{};

struct Example
{
Type *& foo(const std::string &s)
{
std::map<std::string, Type *>::iterator it = map.find(s);
if(it != map.end())
{
return it->second;
}
else
{
throw std::runtime_error("blah");
}
}
private:
std::map<std::string, Type *> map;
};



Can you post a minimal* example that exhibits the behaviour you are seeing?

* This means I should be able to copy and paste the code into my IDE and build it. No external headers etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!