I'm relearning C++, and it's a bit different than I remember in some ways. For clarity: my use of C++ is C plus classes, namespaces, and operator overloading--never delved into templates, or multiple inheritance.
Consequently, it took me a minute to work out why I could do the following and get what I wanted rather than what I expected:
Code:
//I've got a couple little questions to throw in here while I'm at it.
//This first include statement baffles me--I get the idea from looking it up.
//What baffles me is my compiler behavior regarding it.
//If I don't use it, the compiler throws an error and won't compile.
//If I do use it, a mouseover tells me it can't find/open the file
//but it does compile.
//The cleanest solution to me is to track down whatever setting is throwing the error
//and change it, then stop calling it--but it's fucking weird that Visual Studio
//has it on as a default but doesn't have the file it needs to do it.
#include <stdafx.h>
#include <iostream>
using namespace std;
int main()
{
//auto is new feature that coming from Python I find amusing and dangerous. I used it to figure out
//how to invoke a cleaner string I didn't have to typecast to get into cout. Thus I discovered
//const char * var--which is what auto selected for me.
auto message = "Hello.\n\nTest\n\n";
cout << message;
message = "A new String\n\n";
cout << message;
return 0;
}
The interesting thing in this is being able to change the value of 'message'. I call it Pointer Trick #1. Your pointer is constant, the value it points to is not.
What I'm trying to do is Pointer Trick #2. You instantiate a const. Then, you assign it's address to another pointer, and change the value at the address of the pointer, thereby also changing the value of the original const. Presently I'm trying to do it with a const char var[], but can't get the compiler to play ball. I don't know if I'm butting against the compiler or the language, though I'm planning to try the experiment again tonight with Notepad++ and GCC--unless someone can explain what my screwup is.
Code:
const char mark[] = "Test message 1\n\n";
const char * sneak = mark;
cout << mark;
sneak = "New message!\n\n"
cout << mark;
I'm probably overlooking something simple and stupid, but it's part of the basic pointer hat-trick.
Bookmarks