Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line class definition?

I'm updating some code and while I was working in a header, I came across the following line.

.
.
.
class HereIsMyClass;
.
.
.

That's it. It's just one line that precedes another, longer class definition. HereIsMyClass is in fact another class somewhere else, but I don't understand why this line is written here. What does it do?

like image 784
samoz Avatar asked Jun 02 '09 14:06

samoz


People also ask

How do you define a class?

a class describes the contents of the objects that belong to it: it describes an aggregate of data fields (called instance variables), and defines the operations (called methods). object: an object is an element (or instance) of a class; objects have the behaviors of their class.

How do you define a class in C?

C Classes A class consists of an instance type and a class object: An instance type is a struct containing variable members called instance variables and function members called instance methods. A variable of the instance type is called an instance.

How do you write a one line function?

Summary: The most Pythonic way to define a function in a single line is to (1) create an anonymous lambda function and (2) assign the function object to a variable name. You can then call the function by name just like any other regularly-defined function.

How do you define a one line function in Python?

It starts with the keyword lambda , followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. For example, lambda x, y: x+y calculates the sum of the two argument values x+y in one line of Python code.


1 Answers

This line in C++ is a forward declaration. It's stating that at some point in the future a class named HereIsMyClass will likely exist. It allows for you to use a class in a declaration before it's completely defined.

It's helpful for both breaking up circularly dependent classes and header file management.

For example

class HereIsMyClass;

class Foo { 
  void Bar(HereIsMyClass* p1) ...
};

class HereIsMyClass {
  void Function(Foo f1);
}
like image 144
JaredPar Avatar answered Sep 22 '22 21:09

JaredPar