473,480 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

member variables of Types without default constructor

LK
Why wouldn't c# catch the following problem at compile
time (as C++ does)?

class EmbClass
{
public int TestVar;
public EmbClass( int i )
{
TestVar = i;
}
}
struct ComplexStruct
{

/* **************PROBLEM************

Not saying here that EmbClass does not have a default
constructor!
*/
public EmbClass DefaultsHow;
public int m_j;
}

static void Main( )
{
ComplexStruct CS = new ComplexStruct( );

//correctly throws a Null-Reference exception
int i = CS.DefaultsHow.TestVar;
}
Jul 21 '05 #1
2 1494
Why wouldn't c# catch the following problem at compile
time (as C++ does)?


Because it's usually not a problem. And C++ classes have different
semantics than managed classes (they are more like value types).

You get a NullReferenceException because the DefaultHow member hasn't
been assigned an object reference. That has little to do with the fact
that the EmbClass lacks a default constructor, since there are many
other ways you could get a valid EmbClass object reference.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #2
LK <an*******@discussions.microsoft.com> wrote:
Why wouldn't c# catch the following problem at compile
time (as C++ does)?


Because C++ needs an actual instance; C# only needs a reference, and
the default value of the reference is null. In other words, C++ would
create an instance in ComplexStruct, but C# wouldn't even if there were
a parameterless constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3497
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
5
3304
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
30
6758
by: Javaman59 | last post by:
I come from a background of Ada and C++ programming, where it was considered good practice to explicitly initialize every variable. Now that I'm programming in C# I think that it would be best...
1
227
by: LK | last post by:
Why wouldn't c# catch the following problem at compile time (as C++ does)? class EmbClass { public int TestVar; public EmbClass( int i ) { TestVar = i; }
1
1439
by: subramanian | last post by:
Consider the following program: #include <iostream> #include <string> class Test { public: Test(const std::string &val); private:
15
3810
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
6
3212
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
10
2390
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
13
11414
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.