473,766 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloaded constructor, access zero-argument constructor

This is probably a C#-101 type question, but I've struck out on finding
an answer. I am setting up a class with a few overloaded constructors.
Inside those constructors, I would like to call the "default"
zero-argument constructor. The compiler gives me an error
"PC_StreamBuffe r is a type but is used like a variable". I've tried
adding "this." in front of references to PC_StreamBuffer (), but that
gives another compiler error. A simplified version of the class is as
follows:

class PC_StreamBuffer
{
private byte[] data;

public PC_StreamBuffer ()
{
length=0;
data=new byte[1000];
}

public PC_StreamBuffer (byte[] vals, int valsLength)
{
PC_StreamBuffer ();
this.Set(vals, valsLength);
}

public PC_StreamBuffer (byte[] vals)
{
PC_StreamBuffer ();
this.Set(vals);
}

<remainder of class methods removed>
}

Thanks,
Mike

Nov 17 '05 #1
3 3319
mblatch <mb*****@yahoo. com> wrote:
This is probably a C#-101 type question, but I've struck out on finding
an answer. I am setting up a class with a few overloaded constructors.
Inside those constructors, I would like to call the "default"
zero-argument constructor. The compiler gives me an error
"PC_StreamBuffe r is a type but is used like a variable". I've tried
adding "this." in front of references to PC_StreamBuffer (), but that
gives another compiler error. A simplified version of the class is as
follows:


<snip>

See http://www.pobox.com/~skeet/csharp/constructors.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
You call one constructor from another like this :

public PC_StreamBuffer ()
{
length=0;
data=new byte[1000];
}

public PC_StreamBuffer (byte[] vals, int valsLength) : this()
{
this.Set(vals, valsLength);
}

public PC_StreamBuffer (byte[] vals) : this()
{
this.Set(vals);
}

"mblatch" wrote:
This is probably a C#-101 type question, but I've struck out on finding
an answer. I am setting up a class with a few overloaded constructors.
Inside those constructors, I would like to call the "default"
zero-argument constructor. The compiler gives me an error
"PC_StreamBuffe r is a type but is used like a variable". I've tried
adding "this." in front of references to PC_StreamBuffer (), but that
gives another compiler error. A simplified version of the class is as
follows:

class PC_StreamBuffer
{
private byte[] data;

public PC_StreamBuffer ()
{
length=0;
data=new byte[1000];
}

public PC_StreamBuffer (byte[] vals, int valsLength)
{
PC_StreamBuffer ();
this.Set(vals, valsLength);
}

public PC_StreamBuffer (byte[] vals)
{
PC_StreamBuffer ();
this.Set(vals);
}

<remainder of class methods removed>
}

Thanks,
Mike

Nov 17 '05 #3
Thanks to you both for the quick fix and references. That worked. I
appreciate it.

Mike

Nov 17 '05 #4

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

Similar topics

20
37397
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The C++ Programming Language_, section 11.2 (page 263), these three operators 'take a name, rather than a value, as their second operand and provide the primary means of referring to members. Allowing them to be overloaded would lead to subtleties.'"...
10
1987
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now my doubt is , if u have something like p->a ....where p(say) is a pointer of a user defined type, then u can overload this because p is determined at runtime ???is this right ??? also if "a" is an object of the some user defined class,
44
2430
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case statement: void print_structs(void * struct_argument, enum struct_type stype) { switch(stype) { case STRUCT_TYPE_1:
32
3913
by: John Davis | last post by:
One interesting observation I found is VB6 or VB.NET overloads = opeartor. i.e. = operator has 2 meanings. Case 1: relational operator. In other languages, usually use == instead. If a = b Then statement Case 2: assignment operator a = b Just my observation. Please discuss.
2
2891
by: Claudius | last post by:
Hello, I have written a class A with the access operator(int) overloaded by a A-const version which returns an int by value: ------------------------------------------------ #include <iostream> using namespace std;
12
2224
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer to this struct to other vendor routines. The struct describes a complex time-varying object, but it basically boils down to an integer, and there are access routines to get to the current value of this integer. My problem is: 1) Our code is...
5
1704
by: mike3 | last post by:
Hi. Is this a good idea?: <begin code> /* Addition operator: += */ const BigFix &BigFix::operator+=(const BigFix &rhs) { ErrorType err; int lhs_sign = sign, rhs_sign = rhs.sign;
4
3379
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
36
2038
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are there any other no-overhead ways that a contained class can access its container? The obvious choice of passing (a pointer or a reference to the container) to the contained class is not a no-overhead solution, it requires both memory and time. I am...
1
1258
by: =?Utf-8?B?QWxhbWVsdQ==?= | last post by:
I have overloaded a new operater for a class that returns void * and i have created an object for that class in heap as below. CMyClass *pMyClass = new CMyClass(); // Here though i haven't explicitly casted void* to CMyClass, the compiler doesnt give error at all. But in c++ for void* to specific type ptr , explicite cast is required. What was the cause for the above statement to work? Regards,
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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 we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.