简体   繁体   中英

C++ Question about Char Strings and Pointers

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)

void logC(const char* message);

Accepts a pointer to a char buffer. This is also known as a C-string (as long as it's null terminated, which yours is, because it's a string literal).

void logCA(const char message[]);

Exactly the same . Don't let the [] fool you: this is literally the same as const char* message . That's not because arrays are pointers: they're not. It's a weird oddity from C history. A function parameter with what looks like "array type" with no dimensions, is treated as a pointer parameter instead. Yuck!

void logS(string message);

Accepts a std::string . These can be constructed from C-strings, and this is happening for you behind the scenes when you pass in your string literal.

Which is best? That's highly subjective. Arguably you want a const std::string_view , which can accept any of these at no cost. But a deep discussion on the differences is out of scope here; it can be found in your book, however.

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

Because cout has a special rule for char* , knowing that you usually want these to be treated like strings, because you do. Instead of printing the pointer's value (a memory address), it dereferences that and prints all the characters in the buffer it points to, ending at the null terminator. It's a feature.

If you use a cast to another pointer type, you can "turn that off", eg cout << (void*)message now you'll see an address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM