copy constructor issue

Feb 3, 2021 at 3:44pm
Hello, I am trying to practice class creation and copy/move constructors etc, but having an issue with my copy constructor. Code below. When I am trying to assign the name to the new name, I am getting the error " expression preceding parentheses of apparent call must have (pointer-to-) function type" but I am not entirely sure what that means.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #pragma once
#include <iostream>
#include "engine.h"

using std::string;

class vehicle
{
private:
	string* ptr_Name;
	string colour;
	int doors;
	engine* Engine;

public:
	vehicle(string P_name, string P_colour, int P_doors, engine* P_engine) : ptr_Name(new string(P_name)), colour(P_colour), doors(P_doors), Engine(P_engine)  {}

	~vehicle() { delete ptr_Name; }

	vehicle(const vehicle& obj)
	{
		ptr_Name(new string(obj.getName()));
	}

	string getName() const;
	string getColour() const;
	int getDoors() const;
	engine* getEngine() const;
	void getDetails() const;
};


the getName() function returns

1
2
3
4
string vehicle::getName() const
{
	return *ptr_Name;
}
Feb 3, 2021 at 4:15pm
1
2
3
4
vehicle(const vehicle& obj)
	{
		ptr_Name(new string(obj.getName()));
	}


should be, as per the other constructor (not tried):

 
vehicle(const vehicle& obj) : ptr_Name(new string(obj.getName())), colour(obj.colour), doors(obj.doors), Engine(obj.Engine) {}


This is the copy constructor, so all the member variables need to be assigned.

Also, unless you use move semantics, pass P_name and P_colour by ref instead of by value to avoid an unnecessary copy.
Last edited on Feb 3, 2021 at 4:19pm
Feb 3, 2021 at 9:32pm
Thank you. For some reason, i thought i as going to have to assign each engine member var separately, I didnt realise I couldnt just do it like that.

I am also trying to work out how I would create the copy constructor for a object that has inherited from vehicle. Do I need to somehow bring in the base class to the inherited class copy constructor?

I am trying something like this, but not sure what to include next with the copy constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include "vehicle.h"

class truck : public vehicle
{
private:
	bool hasBed;
	string* bedType;

public:
	truck(bool P_hasBed, string P_bedType, string P_name, string P_colour, int P_doors, engine* P_engine) : hasBed(P_hasBed), bedType(new string(P_bedType)), vehicle(P_name, P_colour, P_doors, P_engine)
	{}

	truck(bool P_hasBed, string P_name, string P_colour, int P_doors, engine* P_engine) : hasBed(P_hasBed), vehicle(P_name, P_colour, P_doors, P_engine)
	{}

	truck(const truck& obj) : hasBed(obj.hasBed), bedType(new string(obj.getBedType())), vehicle(obj)  {}

	void getDetails() const;
	string getBedType() const;
};



EDIT

i might have solved it with this. But would this way be correct?

 
	truck(const truck& obj) : hasBed(obj.hasBed), bedType(new string(obj.bedType)), vehicle(obj)  {}



Last edited on Feb 3, 2021 at 10:24pm
Feb 3, 2021 at 9:57pm
Why are vehicle::ptr_Name and truck::bedType pointers in the first place? Why not just make them strings? That way you don't have to worry about the memory management.
Feb 3, 2021 at 10:04pm
Why are vehicle::ptr_Name and truck::bedType pointers in the first place? Why not just make them strings? That way you don't have to worry about the memory management.


It is just for practice. I am just wanting to practice making copy constructors etc. I am self learning so just needing to think of different ways to learn everything.
Feb 4, 2021 at 9:40am
 
	truck(const truck& obj) : hasBed(obj.hasBed), bedType(new string(obj.bedType)), vehicle(obj)  {}


No. bedType is of type string* (pointer to string). You need to de-reference the pointer first to get a type string.

 
	truck(const truck& obj) : hasBed(obj.hasBed), bedType(new string(*(obj.bedType))), vehicle(obj)  {}


Also, as bedType is not initilaised in all of the constructors, it should be default initialised.

 
string* bedType {};


Last edited on Feb 4, 2021 at 9:41am
Feb 5, 2021 at 2:20am
Thank you. For some reason it was working without the dereference, but I understand why it is needed.
Topic archived. No new replies allowed.