3 Replies - 493 Views - Last Post: 08 February 2018 - 08:55 AM Rate Topic: -----

#1 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-January 16

How to change pointer ownership?

Posted 08 February 2018 - 05:16 AM

Hello! Im having some issues changing ownership of my pointer. In the code below im trying to change powersource, however it does not seem to work. I think the "new"-keyword has something to do with it. Please enlighten me :)/> Thanks

Main is in the bottom of code:

#include <string>
#include <iostream>
 
/*
**  POWERSOURCE CLASS
*/
class PowerSource {
 
private:
    std::string typeStr;  // A descriptive name
 
public:
    PowerSource(std::string type) :typeStr(type) { };
 
    virtual ~PowerSource() = default;
 
    const std::string& type() const { return typeStr; }
    virtual bool isRunning() const = 0;
    virtual bool tryStart() = 0;    // returns true if success, else false
    virtual void stop() = 0;
};
 
/*
**  GASTURBINE CLASS - INHERITS POWERSOURCE
*/
class GasTurbine : public PowerSource
{
public:
 
    GasTurbine() : PowerSource("GasTurbine") { running = false; power = 0; };
    GasTurbine(std::string name) : PowerSource(name) { running = false; power = 0; };
 
    virtual bool isRunning() const override { return running; };
    virtual bool tryStart() override {
        if (running) {
            std::cout << "The GasTurbine is already running..." << std::endl;
            return false;
        }
        else {
            std::cout << "You have started the GasTurbine." << std::endl;
            running = true;
            return true;
        }
    };  
 
    virtual void stop() override {
        if (running) {
            std::cout << "The GasTurbine has been stopped." << std::endl;
            running = false;
        } else {
            std::cout << "The GasTurbine is not running at the moment." << std::endl;
        }
    };
 
private:
 
    bool running = false;
    int power = 0;
};
 
/*
**  FUELCELL CLASS - INHERITS POWERSOURCE
*/
class FuelCell : public PowerSource
{
public:
 
    FuelCell() : PowerSource("FuelCell") { };
    FuelCell(std::string name) : PowerSource(name) { };
 
    virtual bool isRunning() const override { return running; };
    virtual bool tryStart() override {
        if (running) {
            std::cout << "The FuelCell is already running..." << std::endl;
            return false;
        }
        else {
            std::cout << "You have started the FuelCell." << std::endl;
            running = true;
            return true;
        }
    };
 
    virtual void stop() override {
        if (running) {
            std::cout << "The FuellCell has been stopped." << std::endl;
            running = false;
        }
        else {
            std::cout << "The FuelCell is not running at the moment." << std::endl;
        }
    };
 
private:
 
    bool running = false;
    int power = 0;
 
};
 
/*
**  VEHICLE CLASS
*/
class Vehicle
{
protected:
    PowerSource* powerSource;
 
public:
 
    Vehicle(PowerSource* powerSource) : powerSource(powerSource) {};
    ~Vehicle() {};
 
    bool tryStart() {
 
        if (powerSource->isRunning()) {
            std::cout << "The vehicle is already started." << std::endl;
            return false;
        } else {
            powerSource->tryStart();
            return true;
        }
    };
   
    bool stop() {
        if (powerSource->isRunning()) {
            powerSource->stop();
            return true;
        } else
            return false;
    };
    void setPowerSource(PowerSource* ps) { *powerSource = *ps; }; // Change PowerSource
};
 
/*
**  SUBMARINE CLASS - INHERITS VEHICLE
*/
class Submarine : public Vehicle
{
public:
 
    Submarine(PowerSource* powerSource) : Vehicle(powerSource) {}
};
 
int main()
{
 
    PowerSource *powerSource = new GasTurbine;
    Vehicle *vehicle = new Submarine(powerSource);
 
    vehicle->tryStart(); // Prints GASTURBINE HAS STARTED
    vehicle->stop();
 
    std::cout << "Changing Powersource to Fuel cell." << std::endl;
 
	// I think my issue is that I use the "new"-keyword here. But what else should I use? :)/>/>
    vehicle->setPowerSource(new FuelCell("FuelCell")); // Lets change powersource to Fuel cell
    vehicle->tryStart(); // It still says GasTurbine and not Fuel cell

	// Delete the pointers
	delete powerSource;
	delete vehicle;
    system("pause");
 
    return 0;
}



Here is also link to pastebin if someone prefer that: https://pastebin.com/fUeJvYXU

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to change pointer ownership?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7163
  • View blog
  • Posts: 14,929
  • Joined: 16-October 07

Re: How to change pointer ownership?

Posted 08 February 2018 - 05:38 AM

View PostTotte, on 08 February 2018 - 07:16 AM, said:

Im having some issues changing ownership of my pointer.


There's really no such thing as pointer "ownership" in C++. In Rust, maybe, but in C++ you're on your own. Really, really, on your own: tis the joy of C++.

// certainly Wrong
// void setPowerSource(PowerSource* ps) { *powerSource = *ps; }; // Change PowerSource
// probably right
void setPowerSource(PowerSource* ps) { powerSource = ps; }; // Change PowerSource



Consider what you're doing. A pointer is simply an address to data in memory. You're setting a new address. Also, of note, you're loosing the prior address here. If you aren't deleting that object somewhere else, you've created a memory leak.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,253
  • Joined: 05-May 12

Re: How to change pointer ownership?

Posted 08 February 2018 - 08:34 AM

Or if our OP was writing Modern C++, they wouldn't be using naked pointers. Instead they would be using smart pointers. For objects that should have only one owner, the std::unique_ptr would use std::move() to transfer ownership.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7163
  • View blog
  • Posts: 14,929
  • Joined: 16-October 07

Re: How to change pointer ownership?

Posted 08 February 2018 - 08:55 AM

Heh, I didn't want to get into the smart pointer thing, but you are, of course, correct. For the C++ novice, knowing you can blow your foot off if you're not careful seems just part of the experience.

I actually didn't know the move operation for that, so cool.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1