Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Returning dereferenced pointer from function  (Read 5480 times)

0 Members and 2 Guests are viewing this topic.

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Returning dereferenced pointer from function
« on: January 10, 2013, 10:46:52 pm »
Okay, this is more a question than a problem.
This one:
sf::Texture & ResourceManager::GetTexture(std::string name) {
        return *textures.find(name)->second;
};
returns a reference to an object, but this one:
sf::Texture ResourceManager::GetTexture(std::string name) {
        return *textures.find(name)->second;
};
as far as I understand, it returns copy of an object (correct me if I'm wrong).
So, we have a copy of an object that contains a texture. That is good, let him keep this copy. But when I pass this copy as a parameter:
player=(Player*)layers->GetLayer("lyrDynamicOverlay")->CreateObject(2000+100,300-40,indPlayer,ResMan->GetTexture("Texture"));
 
it loads nothing. Just like there was no texture at all.
So, what happened?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Returning dereferenced pointer from function
« Reply #1 on: January 11, 2013, 08:06:50 am »
The copy that you pass to the function is temporary, and lost when the function returns. So unless you make your own copy of the copy inside the function, the sprite uses an destroyed texture.
Laurent Gomila - SFML developer

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Returning dereferenced pointer from function
« Reply #2 on: January 11, 2013, 02:58:16 pm »
The copy that you pass to the function is temporary, and lost when the function returns. So unless you make your own copy of the copy inside the function, the sprite uses an destroyed texture.
So, I still use copy, not the real object? Should I return pointer then?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Returning dereferenced pointer from function
« Reply #3 on: January 11, 2013, 05:02:43 pm »
Yes, pointer or reference. I don't think you want to copy heavy textures, anyway.

By the way, the (Player*) cast in your code looks wrong. Why do you need it?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Returning dereferenced pointer from function
« Reply #4 on: January 11, 2013, 11:57:20 pm »
Yes, pointer or reference. I don't think you want to copy heavy textures, anyway.

By the way, the (Player*) cast in your code looks wrong. Why do you need it?

To cast pointer to (Player *) type. "Player" is inherited from "Object" and I have "Object" instances in an array.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Returning dereferenced pointer from function
« Reply #5 on: January 12, 2013, 02:13:58 pm »
Do not use C casts. They try to convert source to destination type somehow, including conversions that you may not intend. In C++, the semantics have been split to three different operators, additionally there is dynamic_cast:
// Scalar types or class hierarchies (statically)
double decimal = 4.5;  
int integral = static_cast<int>(x);

Base& base = ...;
Derived& derived = static_cast<Derived&>(base);

// Remove CV qualifiers
const MyClass& constant = ...;
MyClass& variable = const_cast<MyClass&>(constant);

// Re-interpret memory layout
double* pointer = ...;
unsigned char* bytePointer = reinterpret_cast<unsigned char*>(pointer)

// Cast class hierarchies at runtime
Base& base = ...;
Derived& derived = dynamic_cast<Derived&>(base);

In your case, static_cast is the correct choice. But if there is a way to avoid downcasts, you should take it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Returning dereferenced pointer from function
« Reply #6 on: January 12, 2013, 05:51:21 pm »
Do not use C casts. They try to convert source to destination type somehow, including conversions that you may not intend. In C++, the semantics have been split to three different operators, additionally there is dynamic_cast:
// Scalar types or class hierarchies (statically)
double decimal = 4.5;  
int integral = static_cast<int>(x);

Base& base = ...;
Derived& derived = static_cast<Derived&>(base);

// Remove CV qualifiers
const MyClass& constant = ...;
MyClass& variable = const_cast<MyClass&>(constant);

// Re-interpret memory layout
double* pointer = ...;
unsigned char* bytePointer = reinterpret_cast<unsigned char*>(pointer)

// Cast class hierarchies at runtime
Base& base = ...;
Derived& derived = dynamic_cast<Derived&>(base);

In your case, static_cast is the correct choice. But if there is a way to avoid downcasts, you should take it.

Okay, thanks!

 

anything