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

Author Topic: References returning null values?  (Read 9670 times)

0 Members and 2 Guests are viewing this topic.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: References returning null values?
« Reply #15 on: November 02, 2012, 07:07:49 pm »
Shared pointers maybe. But there are simpler ways to deal with that. Manager classes put in well thought locations. See : http://en.sfml-dev.org/forums/index.php?topic=9437.msg64068#msg64068
Quote
the "getSprite" function seems completely useless and a waste of code
Yes!
« Last Edit: November 02, 2012, 07:19:39 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: References returning null values?
« Reply #16 on: November 02, 2012, 07:46:16 pm »
Shared pointers maybe. But there are simpler ways to deal with that. Manager classes put in well thought locations. See : http://en.sfml-dev.org/forums/index.php?topic=9437.msg64068#msg64068
Quote
the "getSprite" function seems completely useless and a waste of code
Yes!

Thanks for the link! It does seem a good idea to just include it in the main game state manager class, then I can just use that to sort of bounce off of to access it. Once I get a few new functions wrote for my window, I may try to take a look at that. :)

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: References returning null values?
« Reply #17 on: November 02, 2012, 07:56:44 pm »

Hey I was just wondering, whats your input on this?

int* EngineWindow::GetCenter()
{
        int *WindowCenter = new int[1]; //[0=X,1=Y]

        sf::Vector2u FullSize;

        FullSize = this->getSize();

        WindowCenter[0] = FullSize.x/2;
        WindowCenter[1] = FullSize.y/2;

        return WindowCenter;
}
 

I remember you saying not to use *new* but it does work with it, I just don't have a clue about how to delete it. It does work without error though. It returns a int Array.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: References returning null values?
« Reply #18 on: November 02, 2012, 08:09:42 pm »
Does it really? Should cause access violation or stack corruption. It's also (possibly) leaking memory. Why can't you just return sf::Vector? There is no reason not to return sf::Vector or if you really need array : std::vector(which is unleaky, self resizing array).
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: References returning null values?
« Reply #19 on: November 02, 2012, 08:15:52 pm »
Does it really? Should cause access violation or stack corruption. It's also (possibly) leaking memory. Why can't you just return sf::Vector? There is no reason not to return sf::Vector or if you really need array : std::vector(which is unleaky, self resizing array).

Yep it somehow works. XD

I'll just use a sf::vector. Should I just return a copy then since its returning data to an outside class and get rid of the pointers?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: References returning null values?
« Reply #20 on: November 02, 2012, 08:17:40 pm »
Yes! All functions that TAKE vector in SFML take const references or references and all functions that return vectors return a copy.
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: References returning null values?
« Reply #21 on: November 02, 2012, 08:22:08 pm »
TAKE vector in SFML take const references or references

???

Not sure what you are talking about there...

This is the code I came up with.... am I right?
sf::Vector2u& EngineWindow::GetCenter()
{
        sf::Vector2u WindowCenter;

        sf::Vector2u FullSize;

        FullSize = this->getSize();

        WindowCenter.x = FullSize.x/2;
        WindowCenter.y = FullSize.y/2;

        return WindowCenter;
}
 
« Last Edit: November 02, 2012, 08:26:20 pm by Flash619 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: References returning null values?
« Reply #22 on: November 02, 2012, 08:27:48 pm »
Functions are declared like this:
void doShitWithVector(const sf::Vector2f& vector);
sf::Vector2f calculateShitVector();
not like this:
void doShitWithVector(sf::Vector2f vector);//waste of memory passingvector by value
sf::Vector2f& calculateShitVector();//cant return reference to temporary vector from inside fucntion scope
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: References returning null values?
« Reply #23 on: November 02, 2012, 08:29:20 pm »
Functions are declared like this:
void doShitWithVector(const sf::Vector2f& vector);
sf::Vector2f calculateShitVector();
not like this:
void doShitWithVector(sf::Vector2f vector);//waste of memory passingvector by value
sf::Vector2f& calculateShitVector();//cant return reference to temporary vector from inside fucntion scope

But I'm not accepting any vectors in my functions. In fact.... I'm not accepting any input to them. They only create and return a vector.......

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: References returning null values?
« Reply #24 on: November 02, 2012, 08:31:02 pm »
Yes. So you return a copy. References are for things that are heavy to copy or for input values to functions.
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: References returning null values?
« Reply #25 on: November 02, 2012, 08:36:22 pm »
Yes. So you return a copy. References are for things that are heavy to copy or for input values to functions.

Oh that makes sense. I just often have difficulty figuring out whats considered a heavy copy. I suppose that would be things like.... sound objects, textures, stuff like that.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: References returning null values?
« Reply #26 on: November 02, 2012, 08:42:58 pm »
Textures, Images, SoundBuffers are Heavy. Sprites, Sounds, Vectors are Light.(this list is not complete..)
Everything is written in SFML documentation usually.
Back to C++ gamedev with SFML in May 2023