I have two files sampleclass.cpp and sampledataclass.cpp along with sampleclass.h and sampledataclass.h

In sampleclass.cpp in the constructor I give a value to a variable

sampleclass.cpp
sampleclass:sampleclass(){
x = "some string value";
}

In sampleclass.h I create x and an instance of the data class
sampleclass.h
CString x;
sampledataclass myDC;

Now in the constructor of the data class I want to be able to set the value of another string to the value of x
sampledataclass.cpp
sampledataclass:sampledataclass(){
CString y = mysampleclass->x;
}

The h file has this
sampleclass.h
sampleclass *mysampleclass;

How do I do this without getting a runtime error

Never define global variables in .h files! You can declare these variables, of course - simply use extern keyword before its type, but define such variables in one and only only cpp file.
As usually, there are better solutions than global variables using, but it's the other story.

The key question in your case: who (and when) must initialize (global?) mysampleclass pointer variable? I don't know the answer because your snippet is too laconic.

It seems you have too cumbersom mechanics for a simple task. May be you present more codes and better your program case description?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.