5 Replies - 280 Views - Last Post: 04 December 2017 - 02:50 PM Rate Topic: ***-- 2 Votes

#1 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 156
  • Joined: 26-August 12

New to C++ - some advice please

Posted 04 December 2017 - 10:29 AM

Hi,

I'm relatively new to C++ and of course it's quite different to the other programming languages I'm proficient in. Before I let myself loose converting my C# project to C++, could you please give advice on whether I've done this correctly, efficiently, properly etc?

Firstly, I've created an enum class, which only has a .h and no .cpp
#pragma once
enum class PacketDirection
{
	SEND,
	RECEIVE
};



The second thing is I've created a base class for future classes to implement. Each derived class will have a serialization function which converts its stored variables to a char array, and it can stringify its variables for easy debugging. I used "protected" as I believe this is best for base classes, to make sure you cannot use the functions from anywhere but a derived class. I don't need to use pointers here because I'm not going to be modifying the returned values.

IGamePacket.h:
#pragma once
class IGamePacket
{
protected:
	unsigned char* serialize;
	std::string stringify;
};



IGamePacket.cpp:
#include "stdafx.h"
#include "IGamePacket.h"
#include <string>

unsigned char* serialize()
{

}

std::string stringify()
{

}



One more thing, when I'm deriving from this base class, which access modifier should I use?
class PacketPlayerJoin : public IGamePacket
{
    ...
}

class PacketPlayerJoin : protected IGamePacket
{
    ...
}


This post has been edited by Rixterz: 04 December 2017 - 10:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: New to C++ - some advice please

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6010
  • View blog
  • Posts: 20,661
  • Joined: 05-May 12

Re: New to C++ - some advice please

Posted 04 December 2017 - 10:36 AM

View PostRixterz, on 04 December 2017 - 12:29 PM, said:

I don't need to use pointers here because I'm not going to be modifying the returned values.

But you are using pointers with your unsigned char * ...
Was This Post Helpful? 0
  • +
  • -

#3 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 156
  • Joined: 26-August 12

Re: New to C++ - some advice please

Posted 04 December 2017 - 10:48 AM

I did think that, but I looked through several pages of google and only saw "char*" when people were talking about char arrays. I tried unsigned char[] serialize, unsigned char serialize[] but the editor kept complaining about syntax errors.

This post has been edited by Skydiver: 04 December 2017 - 10:58 AM
Reason for edit:: Removed unnecessary quote. No need to quote the full post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6010
  • View blog
  • Posts: 20,661
  • Joined: 05-May 12

Re: New to C++ - some advice please

Posted 04 December 2017 - 10:56 AM

There is no need to quote the full post above yours. Just use the big Reply button or the Fast Reply area.

Instead of returning a unsigned char * return a std::vector<unsigned char>.
Was This Post Helpful? 1
  • +
  • -

#5 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 156
  • Joined: 26-August 12

Re: New to C++ - some advice please

Posted 04 December 2017 - 11:17 AM

Must admit I haven't ever seen that reply button before, only Quote+Reply and MultiQuote.

Thanks for the correction, I've now replaced that in my .h and .cpp. Am I doing everything else right though?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6010
  • View blog
  • Posts: 20,661
  • Joined: 05-May 12

Re: New to C++ - some advice please

Posted 04 December 2017 - 02:50 PM

If you are following the C# naming convention where the 'I' prefix indicates an interface, then you should use public inheritance for the sake of consistency. An interface is a contract, and so that contract should be publicly exposed.

That then makes the rest of your interface suspect because you don't have anything publicly exposed. You currently have everything within the interface as protected.

In general, use non-public inheritance for code sharing, and public inheritance for contract sharing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1