I'm fairly new to C++ and still trying to understand Pointers and when they are needed or not. But for this question, I was looking around to creating header files and classes. My first "test" was to just create a class that can handle "Logs" So I did this.
Logger.h
#pragma once
#include <iostream>
using namespace std;
class Logger
{
public:
Logger();
void logC(const char* message);
void logCA(const char message[]);
void logS(string message);
};
Logger.cpp
#include "Logger.h"
Logger::Logger() {
}
void Logger::logC(const char* message)
{
cout << message << endl;
}
void Logger::logCA(const char message[])
{
cout << message << endl;
}
void Logger::logS(string message)
{
cout << message << endl;
}
And then on my main function I just use them like this.
#include "Logger.h"
int main(){
Logger myLogger;
myLogger.logC("Hello");
myLogger.logCA("Alo");
myLogger.logS("Hola");
cout << endl;
system("pause");
return 0;
}
Few questions here. Why would I use one method over the others. And also, on the const char* method, why is this is a pointer to a char it takes a char array as valid and also, if it's receiving an address to the location of a char, why the output is the whole array and not the memory address it's on. (And it also "knows" where to stop, it doesn't print from that address and forever)
const char message[]is really equal toconst char* message.