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

Author Topic: .cpp syntax for reference initialization  (Read 1685 times)

0 Members and 1 Guest are viewing this topic.

hyperworker

  • Newbie
  • *
  • Posts: 8
    • View Profile
.cpp syntax for reference initialization
« on: April 13, 2018, 10:20:08 pm »
I am following a guide on SFML and have met a pitfall. From what I understand, the writer was using psuedocode since his original code was:

class World : private sf::NonCopyable
{
public:
explicit World(sf::RenderWindow& window);


private:
sf::RenderWindow& mWindow;

};

I believe this is psuedocode because you have to initialize a reference first.

So I consulted my Stroustrup book and found that I can do it like this.

    #pragma once
    #include <SFML/Graphics/RenderWindow.hpp>


        class Renderer : private sf::NonCopyable
        {

        public:

                Renderer();  

                Renderer(sf::RenderWindow &newWindow) :renderWindow(newWindow) {}

                ~Renderer();


        private:


                sf::RenderWindow& renderWindow;

        };


But I am missing the syntax for the cpp file, I get errors for these.

        Renderer::Renderer() {}


        Renderer::~Renderer()
        {
        }
 
Quote
'Renderer::renderWindow;' references must be initialized

I've searched for a few hours for the answer, but either I am god awful at using keywords or it's just not an easy topic to find. Any help, suggestions or criticisms are greatly appreciated.
« Last Edit: April 14, 2018, 11:25:43 am by Laurent »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: .cpp syntax for reference initialization
« Reply #1 on: April 13, 2018, 11:33:02 pm »
You're looking for member initialisation lists: http://en.cppreference.com/w/cpp/language/initializer_list