Now you've turned it from invalid syntax, into something that's likely to crash... Think about what you're doing: You're trying to assign the contents of an unassigned pointer to NULL -- in other words, picking some random, arbitrary bit of memory and trying to write 0 into it.
Fortunately, this is a compiler error anyway since 0 isn't a ClassA.
How about:
NULL is like zero, but is of type void, which C++ won't complain about.
Allright, my C++ knowledge are definitively rusty... But I believe, there is a slight inaccuracy in the statements claimed.
Quote:
Originally Posted by Corona688
Of course they're not the same, one of them isn't even valid C++ syntax.
What let you believe this? Using 0 for NULL is perfectly legal in C++, there are actually equal. Stroustrup himself prefers to use "0": Should I use NULL or 0
This line may also work:
as long as the ClassA provides a default constructor.
Even though any other integer isn't. Hooray, more weird special cases to learn in the C++ language
Naturally he'd prefer to use 0, and so not need to include stdio.h, but it's a strange inconsistency for the C++ language to let you assign 0 when it refuses to let you assign any other integer pointer value. That's what the void type used to be for, which is what NULL was of..
Even though any other integer isn't. Hooray, more weird special cases to learn in the C++ language
Naturally he'd prefer to use 0, and so not need to include stdio.h, but it's a strange inconsistency for the C++ language to let you assign 0 when it refuses to let you assign any other integer pointer value. That's what the void type used to be for, which is what NULL was of..
As you mentioned already, C++ is a strongly typed language (or a least claims to be so).
If you try the following assignment:
Then the compiler shall complain that you're trying to perform an invalid conversion from void* to ClassA* ...
Hello All,
I am a learner in C++. I was testing my inheritance knowledge with following piece of code.
#include <iostream>
using namespace std;
class base
{
public :
void display()
{
cout << "In base display()" << endl;
}
void display(int k)
{... (2 Replies)
I have the two class definition as follows.
class A { public: int a; };
class B : virtual public A{ };
The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Hi all!
I am trying to register a device in an existing device class, but I am
having trouble getting the pointer to an existing class.
I can create a class in a module, get the pointer to it and then use
it to register the device with:
*cl = class_create(THIS_MODULE, className);... (0 Replies)
Hi,
I believe the next code is wrong:
class Egg {
Egg e;
int i;
Egg(int ii=0) : i(ii) {}
};
because you would end up with an endless definition (memory allocation) of Egg objects, thus int i.
Ok, so God Eckel proposes for a singleton:
class Egg {
static Egg e;
int... (5 Replies)
I'll be gratefull for any help. Thanks.
:)
This is the non class type error:
# g++ -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlpp -L/usr/lib/mysql -L/usr/local/lib/mysql++ loaddsgsports.cpp -o loaddsgsports
loaddsgsports.cpp: In function âint outputToImport(const char*, const char*,... (1 Reply)
Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento)
typedef struct elemento
{
char name;
char value;
List<struct elemento> ltElementos;
... (7 Replies)
im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
int my_malloc(int size, char **pmem)
{
*pmem=(char *)malloc(size);
if(*pmem==NULL)... (11 Replies)
Hi
We are using a code generator for initializing structures with the #define macro. Compiling it with the GCC 2.8.1 (with -ansi) it OK. But when we are using the SUN C 5.0 compiler it screams.
Following is a code sample:
#include <stdlib.h>
#include <stdio.h>
typedef struct TEST3 {... (4 Replies)