简体   繁体   中英

Constructor not called c++

I have this code in a DLL:

Game::Game(int width, int height)
   : world(width, height),
     renderer("Game", 800, 600),
     font(),
     text(),
     keyboard()
{
    // Code
}

keyboard is a pointer to a Keyboard object. The code inside the constructor is called, but the keyboard object remains null. Why is the object not being created?

The keyboard constructor:

Keyboard() {
 A = new Key('a');
 B = new Key('b');
 C = new Key('c');
 D = new Key('d');
 E = new Key('e');
 F = new Key('f');
 G = new Key('g');
 H = new Key('h');
 I = new Key('i');
 J = new Key('j');
 K = new Key('k');
 L = new Key('l');
 M = new Key('m');
 N = new Key('n');
 O = new Key('o');
 P = new Key('p');
 Q = new Key('q');
 R = new Key('r');
 S = new Key('s');
 T = new Key('t');
 U = new Key('u');
 V = new Key('v');
 W = new Key('w');
 X = new Key('x');
 Y = new Key('y');
 Z = new Key('z');

 ZERO = new Key('0');
 ONE = new Key('1');
 TWO = new Key('2');
 THREE = new Key('3');
 FOUR = new Key('4');
 FIVE = new Key('5');
 SIX = new Key('6');
 SEVEN = new Key('7');
 EIGHT = new Key('8');
 NINE = new Key('9');

 ARROW_UP = new Key(' ');
 ARROW_DOWN = new Key(' ');
 ARROW_LEFT = new Key(' ');
 ARROW_RIGHT = new Key(' ');

 SHIFT = new Key(' ');
}

How do I cause the keyboard constructor to be called?

if keyboard is a pointer the syntax should be

Game::Game(int width, int height)
  : world(width, height),
    renderer("Game", 800, 600),
    keyboard(new Keyboard)
{ ... }

The same also should be done for text / font if they're also pointers.

I don't want to offend you, but this is a quite basic question about how pointers/instances work in C++. If you're learning C++ by writing and trying code before reading about the language then please consider investing some time on reading first instead.

The reason is that C++ is an horrible language to learn by experimentation for two main reasons:

  1. Not everything is logical. Much of C++ rules are a consequence of the evolution of the language and of committee decisions. The most obvious answer to a problem is often NOT the correct one in C++. The only way to know the language is to read about it.

  2. The language main philosophy is that programmers never make mistakes. In other languages when you make a mistake a "runtime error angel" stops the program and tells you what went wrong. In C++ instead when you make a mistake an "undefined behavior daemon" takes the legal ownership of the code and will try hard to cause you the most possible damage, for example by making the program running anyway silently and nicely for a while until you decide to show it to someone, and only crashing it at that point.

The mix 1 + 2 is very dangerous: the language is complex and you will not be told about mistakes. Do yourself a favor instead and read first a good C++ book cover to cover.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM