This question already has answers here:
What are forward declarations in C++?
(8 answers)
Closed 6 years ago.
I've got a namespace, with a class inside. As shown below;
class testClass {
public:
testClass() { std::cout << "neat" << std::endl; };
~testClass() { };
void print() {
std::cout << "printOne" << std::endl;
}
};
namespace test {
class testClass;
class testClassTwo {
public:
void printTwo() {
std::cout << "printTwo" << std::endl;
}
};
}
I know that I can inherit from the testClass using the normal way of
class testClassTwo : public testClass
But I've seen something along the line of what's within the namespace
class testClass;
I haven't been able to find a solid explanation of what this actually does, i'm assuming inheriting something from the testClass class.
I understand the simple stuff about namespaces such as;
test::testClassTwo classobj;
classobj.printTwo();
I can also compile;
test::testClass;
but can't actually do anything with it.
Would anyone be able to forward me to some reading material or a quick explanation of what's actually going on when I do this?
I haven't been able to find a solid explanation of what this actually does, I'm assuming inheriting something from the testClass class.
That's an incorrect assumption. class testClass is a forward declaration of testClass data type. It lets you declare pointers and references to testClass objects without importing the corresponding header.
More information on forward declarations can be found in this Q&A.
It depends on the context.
Where is it in the code?
In the code you pasted, it declares (tells the compiler) that a class called testClass exists in the test namespace.
(Read more: forward declaration)
It should compile fine, but linking would throw an undefined rederence error.
There is no definition for test::testClass.
class testClass;
This is nothing more than a forward declaration. Usually used to prevent circular dependencies.
Related
This question already has an answer here:
Why is there an injected class name?
(1 answer)
Closed 9 months ago.
I accidentally noticed that you can pointlessly use :: operator to reefer back to the original struct/class again, for example:
#include <iostream>
struct Test
{
static const int x = 5;
};
int main()
{
std::cout << Test::x; // makes sense
std::cout << Test::Test::x; // still works?
std::cout << Test::Test::Test::x; // okay...
return 0;
}
This can be stacked more and more. However, you can't construct an object this way, because compiler doesn't recognize it as a type, but interprets it as a class::constructor.
Test::Test Obj; // error, not a type
Although if there was a nested class inside, then you could still reference it this way and successfully construct an object.
I was wondering what leads to this, is this a side effect of something else? It seems totally useless but made me curious.
This is injected class name, simmilar question was asked here
This question already has answers here:
What are the rules for calling the base class constructor?
(10 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Hello World";
}
};
class B:public A{
public:
B(){
cout << "World";
}
};
int main(){
B obj1;
return 0;
}
Why does this program print Hello WorldWorld, shouldn't it print World because I have created object of class B, so why is the constructor of A being called?
Conceptually, a base class becomes an unnamed sub object in the derived class, whose members are available in the same scope without extra qualification, and must be initialized.
You cannot avoid that initialization - which means a relevant constructor will be called or if it cannot be constructed then compiler will not allow you to inherit.
What you probably mean is whether the constructor should have overridden the base version, the simple answer is it cannot. If you want that effect - which will not work in constructors in a common sense way - you need to use virtual functions and overriding.
This question already has answers here:
Can I use `abstract` keyword in C++ class
(12 answers)
Closed 4 years ago.
I was told, the way I declared the Abstract-Class is wrong or not used in C++ but it still works.
I couldn't find any useful source to create an opinion about this so I want to know if it's unusual/wrong/right to programm it this way.
#pragma once
class TestClass abstract
{
public:
void test();
TestClass();
~TestClass();
};
int main()
{
std::cout << "Hello World!\n";
TestClass baum;
}
Error (active) E0322 An object of type "" TestClass "" of an abstract class is not allowed: test
I can't create any object of "TestClass" so my guess is it works but my teacher told me it's wrong.
There is no explicit keyword to make your class abstract in C++. Your code shouldn't compile because abstract isn't a C++ keyword, you're probably using a compiler extension to not have it trip over that keyword.
The way you create an abstract class in C++ is by declaring one or more pure virtual functions, your test for example:
virtual void test() = 0;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Most vexing parse: why doesn't A a(()); work?
I have two classes in file1.h:
class ZoneRecord {
public:
//a lof of stuff here
};
class RegisterRecord {
public:
RegisterRecord(ZoneRecord rec); //this function register object rec in a fabric
};
And file2.cpp has:
#include "file1.h"
class MockZoneRecord: public ZoneRecord {
public:
MockZoneRecord(): ZoneRecord() {}
};
RegisterRecord mockrecord_register(MockZoneRecord());
This code compiles perfectly, except one thing. It says that mockrecord_register is a declaration of a function. But I actually wanted to create an global object of type RegisterRecord with name mockrecord_register. How to explicitly tell to compiler that this is not a function prototype, but an object?
You are experiencing the most vexing parse.
One way to solve this is to use copying, like
RegisterRecord mockrecord_register = RegisterRecord(MockZoneRecord());
Another is the use of parenthesis like in the answer by yuri kilochek.
If your compiler is C++11 compatible, you could use this construct:
RegisterRecord mockrecord_register{MockZoneRecord()};
Place parenthesis around argument:
RegisterRecord mockrecord_register((MockZoneRecord()));
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want to ask where in C++ is the right place to instantiate a instance-variables? I think it should not be in the class declaration, but otherwise I don`t see any disadvantages apart from poor object-oriented design:
class A{ member m; };
I think it should better be like:
class A{ extern member m; };
But I don`t know how to realize it without a pointer like this:
class A{ member* m };
A::A(){ m = new member; }
Is there a "clean solution" to realize this on the stack (without using pointers)?
You can use the constructor initialization list to construct all your member variables as you need.
A::A(const member& memberArg)
: m(memberArg)
{ }
Look at this for more info.
I think you have a misunderstanding of how objects are instantiated. If all you do is declare a class, no member variables are actually instantiated. It isn't until you construct an instance of that class that its member variables exist.
Here's an example to show when a member object gets instantiated:
class ClassA
{
public:
ClassA() { std::cout << "Hello!\n"; }
};
class ClassB
{
public:
ClassA objA;
};
int main()
{
// do some work
ClassB objB; // here, a ClassB object is created, and with it its member ClassA object, so "Hello!" is printed
return 0;
}
As to exactly how you specify what kind of ClassA object to create if its constructor requires arguments, the other answers do a fine job explaining it.
I think it should not be in the class declaration, but otherwise I don`t see any disadvantages apart from poor object-oriented design:
class A{ member m; };
What in your mind makes this poor OO design? This is the preferred mechanism in C++.
I think it should better be like:
class A{ extern member m; };
This isn't valid code. Qualifying member data with a storage class specification such as extern is illegal.
But I don`t know how to realize it without a pointer like this:
class A{ member* m; };
A::A(){ m = new member; }
That will work, but why do that? It looks to me like you are trying to import a Java POV into C++. Everything is allocated, and everything is a reference in Java. In many (most!) cases there is no reason to allocate data members in C++. All it does is add an unneeded indirection and add a place where memory can leak.
You want to use member initializers: they are the only way to initialize class members that have a constructor that requires parameters, and the cleanest way to initialize other class members.
If you have class A and a member m with a constructor:
class A { member m; }
You want
class A { member m; A(); }
A::A()
: m(<constructor params>)
{
}
You would declare your instance variables in your .h file:
A.h
class A {
public:
A();
private:
int value;
double someOtherValue;
}
You can instantiate them in your .cpp file like so:
A.cpp
A::A(): value(5), someOtherValue(10.0)
{
...
}
If the member object is to be totally controlled by the enclosing A object, your first example is the proper way to do it. It does have the downside of requiring a complete definition of member at the point where A is defined.
You could check out the pimpl idiom to reduce coupling, but that still requires the object to be heap-based and not stack-based.