[SOLVED] child class having the same function name as parent

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
bobloblaw
Posts: 100
Joined: April 15th, 2013, 3:05 am
Location: Earth, USA, California, SF Bay Area
Contact:

[SOLVED] child class having the same function name as parent

Post by bobloblaw » January 26th, 2018, 2:55 pm

Hello all, hope 2018 finds you well.

Long time no chat, I've been on lurking from time to time however. I've been working on my own 3d engine in modern opengl and I keep coming back to a c++ issue and I hope the great minds here can help.

So I have a class Sun, this has code that keeps track of the time of day rotates the sun graphic around the world, vec3 position etc. The sun is drawn as a 2d quad made of 2 triangles, I call this a billboard. What make s this special is it always orients itself to the camera so you see it's face always.

Sun is a public child of Billboard since the sun is a billboard in my engine. Each class Sun and Billboard have initialize() functions.

From the main game however I can only call sun.initialize();

The Billboard class's initialize's job is to take an argument that is the file path of the .png (a.k.a. texture) that is displayed on the quad.

The way I have gotten around my problem is changing the name of the billboard.initialize() to initializeBill();

Now since they are different names I can call the billboard initialize() function from inside the Sun.initialize function().

There has to be a better way to solve this parented classes with same function name issue.

Code: Select all

class Sun : public Billboard
{
	public:
		Sun();
		~Sun();
		void initialize( float positionX, float positionY, float positionZ, const char* filePath );
       ...
}

void Sun::initialize( float positionX, float positionY, float positionZ, const char* filePath )
{
	this->setPosition( positionX, positionY, positionZ );
	this->initializeBill( filePath );
}
Thoughts?

~Logan
Last edited by bobloblaw on January 26th, 2018, 3:50 pm, edited 1 time in total.
Here for your Artistic needs, Pixel or Vector I love it all. Check it out - Click Me

User avatar
bobloblaw
Posts: 100
Joined: April 15th, 2013, 3:05 am
Location: Earth, USA, California, SF Bay Area
Contact:

Re: Issue: child class having the same function name as pare

Post by bobloblaw » January 26th, 2018, 3:41 pm

Seems online suggests Billboard::initialize(); going to test it.

https://stackoverflow.com/questions/357 ... s-function
Here for your Artistic needs, Pixel or Vector I love it all. Check it out - Click Me

User avatar
bobloblaw
Posts: 100
Joined: April 15th, 2013, 3:05 am
Location: Earth, USA, California, SF Bay Area
Contact:

Re: Issue: child class having the same function name as pare

Post by bobloblaw » January 26th, 2018, 3:49 pm

Yep that did it, calling Billboard::initialize; from inside of Sun.initialize(); I know I solved the issue before anyone could reply but thanks for reading and being there for the journey :)
Here for your Artistic needs, Pixel or Vector I love it all. Check it out - Click Me

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: [SOLVED] child class having the same function name as pa

Post by albinopapa » January 27th, 2018, 12:02 am

or

Code: Select all

class Billboard
{
public:
      Billboard();  // Do whatever for default or = default;

protected:
      // Protected if you aren't going to create Billboard objects directly, but only through children
      // Otherwise, should be public
      Billboard( std::string filePath ) // for loading the image data

private:
};
class Sun : public Billboard
{
   public:
      Sun();
      Sun(float positionX, float positionY, float positionZ, std::string filePath );
      ~Sun();
       ...
};

Sun::Sun(float positionX, float positionY, float positionZ, std::string filePath )
      :
      Billboard( std::move( filePath ) ),
      position( positionX, positionY, positionZ )
{
      
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: [SOLVED] child class having the same function name as pa

Post by cyboryxmen » January 27th, 2018, 12:57 am

I'd have to agree with Albinopapa here. Initialise() functions don't obey RAII principles. It's more optimal to do things through the constructor.
Zekilk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: [SOLVED] child class having the same function name as pa

Post by chili » January 27th, 2018, 4:44 am

Yes, seconded. Initialize functions are a kind of code stink, and in the vast majority of cases you can do without them, and refactoring to do so will improve the quality of your code.

And welcome back bobloblaw, long time no see :)
Chili

User avatar
bobloblaw
Posts: 100
Joined: April 15th, 2013, 3:05 am
Location: Earth, USA, California, SF Bay Area
Contact:

Re: [SOLVED] child class having the same function name as pa

Post by bobloblaw » January 27th, 2018, 10:07 am

Thanks guys! I have seen a lot of code examples with initialize functions and my inner chili said, "why not just use the constructor?" I thought they may have a reason to initialize the data of an object at a different time than at the creation of said object. Possibly in a vector of objects or some reason. Typical, thought they knew what they were doing and I was in the wrong. Shouldn't doubt my inner chili. Refactoring shouldn't take too long actually :) thanks again.
Here for your Artistic needs, Pixel or Vector I love it all. Check it out - Click Me

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: [SOLVED] child class having the same function name as pa

Post by albinopapa » January 28th, 2018, 5:44 am

With the introduction of move semantics, no need to use the initialize functions. You can always just assign a new object to the existing.

T obj;
// do stuff until not needed,
obj = T( ctor_params );

As long as your class is default constructable, you should never need to use init functions.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply