473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calling Base Class' Ctor

BDB
Hi,

I'm trying to call a base class' constructor. This is giving me an error:
Use of keyword 'base' is not valid in this context.

....

public class B : A
{
B( int i )
{
base( i ) //Calling A's ctor of same declaration
}
}
Thanks,
Bryan
Feb 16 '06 #1
3 2585
You have to call it before the function body, like so:
public class B : A
{
B( int i ) : base( i )
{
}
}

Good luck,
Jared

Feb 16 '06 #2
BDB

"BDB" <bd*@reply.to.group.com> wrote in message
news:Ou**************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm trying to call a base class' constructor. This is giving me an error:
Use of keyword 'base' is not valid in this context.

...

public class B : A
{
B( int i )
{
base( i ) //Calling A's ctor of same declaration
}
}
Thanks,
Bryan


Found it.

public class B : A
{
B( int i ) : base( i ) {}
}
Feb 16 '06 #3
BDB <bd*@reply.to.group.com> wrote:
I'm trying to call a base class' constructor. This is giving me an error:
Use of keyword 'base' is not valid in this context.

...

public class B : A
{
B( int i )
{
base( i ) //Calling A's ctor of same declaration
}
}


I see you've found the answer to this one - but if you have any other
questions about constructors, see
http://www.pobox.com/~skeet/csharp/constructors.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '06 #4

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

Similar topics

7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
27
by: tuvok | last post by:
Is it correct that the virtual dtor of base gets called implicitly? Here's some code to demonstrate what I mean: Class B has a virtual destructor, so has class D which is derived from B. Deleting...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
7
by: Jo Vermeulen | last post by:
Hello, I was wondering how I could call the base class constructor from a derived class constructor. In Java I could do something like super(parameter); I tried base(parameter);
6
by: hazz | last post by:
I have a class/constructor hiearchy that functions correctly as illustrated below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client instantiates object D, eg. D m_D = new...
8
by: Alex Vinokur | last post by:
Microsoft C++ Version 13.10.3077 Why is virtual function init() called in constructor here? ====== foo.cpp ====== #include "iostream" using namespace std; class Base {
8
by: Mike - EMAIL IGNORED | last post by:
I have a class that may or may not be inherited. Its constructor calls a function that should be called only if the class is not inherited. Is there a way to tell, other than simply passing a...
4
by: Fedor Semenov | last post by:
How can I call a base class' constructor from a derived class. Suppose I have: class Button // Base class { public: Button(void) { // Register classes, create the button, etc. } };
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.