C++ Is "class Name*" a class pointer? - class

I was watching an unreal tutorial and encountered this line of code:
class UStaticMeshComponent* Pickup;
It is a forwards declaration.
I have been studying c++ for a while and have not encountered anything like this before. I know about pointers and references, but I never seen this format: class Name*. Are we creating a class pointer to another class? I tried searching for class names followed by a *, but the only result that appeared were about data types and pointers.
public:
APickUp ();
virtual void BeginPlay () override;
virtual void tick ( float DeltaSeconds );
private:
class UStaticMeshComponent* Pickup;

class UStaticMeshComponent* Pickup;
This declares the type class UStaticMeshComponent and also declares the variable Pickup of type pointer to UStaticMeshComponent. So the above code is more or less equivalent with:
class UStaticMeshComponent;
UStaticMeshComponent* Pickup;
Because at this point you don't have the definition of UStaticMeshComponent (just the declaration), UStaticMeshComponent is considered an incomplete type. There are a few things you can do with an incomplete type. One of them is declare pointers to them.

When you are only using a pointer to a type, the type is not required to be complete, so this works fine even if UStaticMeshComponent has not been defined. That means when this header is included in one of your cpp files, it should compile a bit faster than if Pickup.h were to include the header where UStaticMeshComponent is defined.
This speed up could be substantial depending on how big that header is, and how many times the include is avoided during compilation.

The big confusion here is the difference between:
class UStaticMeshComponent* Pickup;
and
class UStaticMeshComponent *Pickup;
and even this:
class UStaticMeshComponent * Pickup;
, as in the example http://www.cplusplus.com/doc/tutorial/classes/#pointers_to_classes .
The difference is - bad practice. All supposed to mean the same thing but only the second example is correct, yet all of them work.
At least according to this:
https://users.ece.cmu.edu/~eno/coding/CppCodingStandard.html
place the * close to variable name not pointer type
I am a novice, I might be wrong. Please correct me if i am wrong.

Related

In C++, how can I create an instance of zoned_time as a class member?

I'm new to C++ and am exploring the use of timezones in chrono, specifically zoned_time.
I have only one issue with it: I want to use an instance of zoned_time as a member in a class, but no matter how I alter the syntax, the compiler doesn't like it.
#include <chrono>
#pragma once
class Event
{
private:
std::chrono::zoned_time tz;
public:
Event();
};
I've created a header file containing the code above, but it says the argument list for the class template is missing. I assume that means it wants me to initialize it, but I don't want to because it's a header file. As I said, I messed around quite a bit with the syntax of tz, combined with a good bit of research. Unfortunately, since I'm new to the language I really don't know what to look for online.
So my goal here is to be able to create a typed container within Event which will store the zoned_time information. I'm not picky on how I achieve that goal (I suspect some black-magic pointer trickery will be the solution). Thank you for the input.
Your error message mentions the word "template", indicating it's not about constructor arguments (initializer), but about generic type arguments (template parameters).
According to https://en.cppreference.com/w/cpp/chrono/zoned_time this class is declared as:
template <
class Duration,
class TimeZonePtr = const std::chrono::time_zone*
> class zoned_time;
So it seems like you need a duration type to use (there is a default for the time zone pointer type). So your line should probably say something like
std::chrono::zoned_time<std::chrono::seconds> tz;

Incomplete type not allowed in C++

I am trying to recreate the data type of array for experimental purposes. I created a class "node" with two attributes value(int) and nextvalue(node) to hold the next node. The plan was to chain a number of node objects together and create an array.
here is my code:
class node
{
public :int value;
node nextnode;
};
However, I get the following error in the line node nextnode; : incomplete type not allowed
Like it is some sort of "recursive class" if that even exists.
I have even tried creating a second class named "node2" that was the exact same with the previous one so as to trick the compiler int thinking the class didn't reference itself.
Is there a solution or is this a dead end?
Unlike other language (for instance C#) a class is always a value type, not a reference. So a class cannot have a member of the type of the same class (you would get infinite recursion and a class with infinite size). Also please note that in C++ class and struct are identical (except for the default access modifier)
So you need to use pointers:
#include <memory>
class node
{
public:
int value;
std::unique_ptr<node> nextnode;
};

Haxe: how to declare "static" methods in an Interface?

This question has been asked (and probably answered) in the old Haxe forums on babble ... but it appears that that entire forum system no longer functions. Therefore, I'm asking here:
In Haxe, I need to declare an "Interface" to a class which includes a static function, "instance()." But when I do so:
You can't declare static fields in interfaces
So I remove the word "static" from public function instance() [...], and I get this:
Field instance needed by [...] is missing.
Apparently a "Catch-22." But there obviously must be some easy solution. What is it?
As you stated the language doesn't allow for static fields on interfaces. The choice is intentional. Another thing that doesn't exist is inheriting static fields.
There are several ways to structure your code to avoid such usage that in my point of view it doesn't give you many advantages. A factory pattern or DI approach (I suggest the minject library) seems the most obvious.
Given the comment below go for a typedef instead of an interface:
typedef GetInstance = Void -> Void;
You can pass that typedef around the same as an interface with the advantage that you can use both static and instance methods to satisfy that signature.
Check out the Singleton library. Any class that implements Singleton will automatically declare a static "instance" variable and corresponding getter function.
Note: as of this writing, the Haxelib version (1.0.0) is out of date. Download the Git version instead.

what is the meaning of `Class of ` type declaration?

While going through one of my code, I am stuck on one statement which is as below.
TMyObjectClass = class of TMyObject;
I am a bit confused, and wondering what is the meaning of this statement.
As TMyObjectClass has no declaration above the statement.
and TMyObject is having declaration as below:
TMyObject = class(TObject)
private
//some private member declaration
Public
// some public variables
end;
So, my question is what is the meaning of the statement
TMyObjectClass = class of TMyObject;
and How TMyObjectClass works?
I am a bit new to Delphi, so please help me to get some idea about these type of declaration and there workarounds.
This is a Class Reference.
They are used to work with meta classes. The canonical example is the Delphi streaming framework which uses
TComponentClass = class of TComponent;
This allows for dynamic binding to virtual constructors. The TComponent constructor is virtual. The streaming framework needs to instantiate classes derived from TComponent. It does so something like this:
var
ComponentClass: TComponentClass;
Component: TComponent;
....
ComponentClass := GetComponentClassSomehowDoesntMatterHow;
Component := ComponentClass.Create(Owner);
Now, because TComponent.Create is virtual, this is bound in a polymorphic fashion. If TComponentClass is TButton, then TButton.Create is called. If TComponentClass is TPanel, then TPanel.Create is called. And so on.
The most important thing to realise is that the class that is constructed is determined only at runtime. Note that many languages lack this capability, most notably C++.

Error: 'class name' redeclared as different kind of symbol?

I was facing the same error as asked in this question
I overcome with this error by solution of declaring class ahead of time in my .h file with the class parameter
I am having FFTBufferManager.h and FFTBufferManager.cpp file and using it in HomeView.h and HomeView.mm file
class FFTBufferManager,CAStreamBasicDescription,DCRejectionFilter;
But now I am having error as
#include "FFTBufferManager.h"
#include "aurio_helper.h"
#include "CAStreamBasicDescription.h"
class CAStreamBasicDescription,FFTBufferManager; //here it shows this error
EXpected Unqualified-id befor ',' token
#interface HomeView
{
FFTBufferManager* fftBufferManager;
//it shows erros
EXpected Unqualified-id befor ',' token
ISO c++ forbids declaration of FFTBufferManager with no type
}
#property FFTBufferManager* fftBufferManager;
//shows error
'FFTBufferManager' is not a type
I'm gathering you're using both C++ and Objective-C.
I'd suggest renaming all your .cpp and .m files in which Objective-C and C++ code are meeting to use the extension .mm - this tells the compiler to use "Objective-C++" rules, and will stop a lot of compiler troubles.
Also, it seems CAStreamBasicDescritpion is a C++ class - you'll have to forward-declare it with class CAStreamBasicDescritpion;, not #class CAStreamBasicDescritpion; (note, no "at" sign) - the second form is only for forward-declaring Objective-C classes. This I suspect is the root cause of the particular error you have observed.
EDIT in response to comment: I'm not sure about your first new issue - that should work fine so long as both FFTBufferManager and CAStreamBasicDescription are C++ classes. As to your second one, depending on where exactly that line of code is (CAStreamBasicDescription thruFormat;) you may need to include the header rather than just the forward-declare: you're declaring an instance of CAStreamBasicDescription here, and the compiler needs to know its structure to do so.
You can't declare more than one class at a time.
Change your declarations to
class CAStreamBasicDescription;
class FFTBufferManager;
The compiler is looking for an unqualified-id because it believes that you're declaring a variable of type CAStreamBasicDescription, so it expects a variable name where you gave it a comma.
Looks like you are trying to create a class that already exists in one of the Cocoa frameworks.