0

I have been self teaching myself C++ and having a blast at it too.

I have been working my own AI - an insane project relatable to moving to a country without knowing the language. Immersion has taught me a lot, and having a a major goal with hundreds of micro goals has fueled me forward.

BUT... I find myself with a LOT of questions. I am hoping a few of these can be answered by those in this community.

C++ 2 years ago looks different (IMO) than it does now. I have come up for air and to learn new principles and concepts and am finding that core code I learned seems to be different.

#define pragma once is an example. Is this new? Or did I completely miss this...

My Next curiosity:

Header Files: Is it normal, or proper practice to include classes that are shared between two other classes in a header file? For example:

#include <iostream>
using namespace std;

#ifndef __SerialPort_h__
#define __SerialPort_h__
extern char* portInput;
#endif

#pragma once

#define ARDUINO_WAIT_TIME 1000
#define MAX_DATA_LENGTH 255

#include <windows.h>
#include <iostream>


class SerialPort
{
private:
    HANDLE handler;
    bool connected;
    COMSTAT status;
    DWORD errors;
public:
    explicit SerialPort(const char *portName);
    ~SerialPort();

   // int readSerialPort(const char *buffer, unsigned int buf_size);
    bool writeSerialPort(const char *buffer, unsigned int buf_size);
    bool isConnected();
    void closeSerial();
};

The above class "SerialPort" is defined within 'SerialPort.h' (not the famous one everyone uses) and I am trying to discern if this is standard practice, or has the author of this code lost his way?

Which is the universal, best practice for the following code. And I know answers are out there regarding this, but so many websites now have mixed information:

Cout

cplusplus.com describes cout standards to be as follows:

cout << "main function\n";

Yet time and time again in others' code:

std::cout << "main function\n";

Function Declarations

Last Year, the proper way to declare functions seemed to be:

void someFunction () { some code } Now I am seeing this format all over the place.:

void someFunction::someFunction() { some code }

So my aim is to grasp enough code to get a job programming. But I cannot move in a positive direction towards that aim without have a firm grasp on standardization, and I have tried reading the iso books, but the time I grasp every concept in those I will be 4 years older and 3 new iso documents will have been published.

A Trusted Resource?

So is there a trusted resource that is held in high esteem within the C++ community for deriving reliable, industry applicable, and standardized definitions that can be applied in modern c++ code? And if so could someone be so Kind as to point me in that direction? Specifically for PC/linux libraries.

I don't have any dev/programmer friends, and frankly its nice to have a human offer a reputable response rather than "hoping" whatever resource I find will yield promising results.

So thank you for whatever help, advice and responses you offer. I know your time is valuable.

Cheers, stay safe during this outbreak.

Andrew

  • I would be happy to answer your questions, but not in one post. Try to focus on one question at a time. – Alex May 05 '20 at 20:20
  • #pragma means "this is compiler specific" but that particular pragma just duplicates the #ifndef stuff you have already done (which is good to avoid multiple definitions -- although it's better not to have definitions in a header) – Kenny Ostrom May 05 '20 at 20:22
  • Header files, check this link (https://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards), for `cout`, read upon the standard namespace, for function declarations, read about where I should put my definitions (.h) and declarations (.cpp), and for a trusted resources, GitHub? – Alex May 05 '20 at 20:22
  • Do you know a reliable resource for standardized practices? Preferably a website or free ebook. – Andrew Burke May 05 '20 at 20:22
  • 1
    Like a tutorial? Find a good book (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Alex May 05 '20 at 20:24
  • I'm thinking more along the lines of a website that offers education, kind of like cPlusPlus, but more community endorsed or respected. I feel they are for hobbyists and not for people hoping for careers in the industry. Their code looks nothing like code you find in professional releases. Its almost scary. – Andrew Burke May 05 '20 at 20:26
  • Side note: NEVER use two underscores in a row `#ifndef __SerialPort_h__` for example. double underscore can have special meaning, so all usage is reserved for use in special circumstances. Details and a few other naming rules here: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 May 05 '20 at 20:26
  • `pragma once` is *not* standard C++. – Jesper Juhl May 05 '20 at 20:26
  • Thanks user4581301, I thought so too. I was pretty sure it was supposed to be `#indef SerialPort_h` – Andrew Burke May 05 '20 at 20:27
  • "cplusplus.com describes" - that's a really poor reference site. Try out https://cppreference.com/ instead, for a higher quality one. – Jesper Juhl May 05 '20 at 20:27
  • 1
    C++ tutorials can be dangerous. In order to know whether or not the tutorial is good or not you have to have enough knowledge of the material being covered to be able to tell a good tutorial from a bad one. In an online space there are no quality controls and sometimes it feels like everybody is trying to get their [Dunning-Kruger merit badge](https://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect), so the odds to finding a good tutorial are low. Instead of looking for tutorials look for the field's domain experts and use their tutorials or tutorials recommended by them. – user4581301 May 05 '20 at 20:30
  • [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/5910058) – Jesper Juhl May 05 '20 at 20:32
  • Good advice user4581301, and Jesper. Thanks a lot. – Andrew Burke May 05 '20 at 20:33
  • A note on cplusplus.com vs cppreference: cplusplus tries for a lower barrier to entry. Their documentation is simpler and more direct and easier to read when you know little. As a result short-cuts are taken and it's not always complete or correct. cplusplus can be an acceptable place to start, but if you suspect you're going to have to go deep or you've tried it and encountered problems, fact check it with a better reference. Eventually you'll find yourself going straight to cppreference (or the C++ Standard for the really weird smurf). – user4581301 May 05 '20 at 20:37
  • *C++ 2 years ago looks different (IMO)* Every 3-5 years there is an update to C++. People find stuff that works, works better, and unfortunately didn't work at all. The language is adapted to fit. In 2011 there was a huge update that is slowly filtering into regular use. 2014 and 2017 added significant new functionality, but 2020 promises some pretty big changes that have me rethinking how I'm going to use the language. Some changes you'll never notice. Some you'll never use. Some will only be used by people working on the edges of the language. But yeah. It's different. – user4581301 May 05 '20 at 20:48
  • On `std::`: Give [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) a read. – user4581301 May 05 '20 at 20:50
  • I _highly_ recommend the 2nd Edition of "A Tour of C++" by Stroustrup. He invented C++ in 1979 and I just finished reading it cover-to-cover this morning. Basically, it teaches C++17 (and some 20) as a whole new language. – Aaron D. Marasco May 05 '20 at 20:50

0 Answers0