473,388 Members | 875 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,388 software developers and data experts.

Getting TargetInvocationException when calling a virtual function from a constructor

JP
Hi,

I am facing a strange problem, please take a look at the code below:

public SaveTree()
{
........
......
......//some code
Initranges()
}

protected virtual Initranges()
{
........//some code
.......
}

Somewhere else, i have a derived class:

public SaveSingleTree: SaveTree
{
.........
//constructor not being overridden
}

Now, when i do something like this:

SaveTree tree = new SaveSingleTree()

I get an exception with the following text:
'System.Reflection.TargetInvocationException' occurred in mscorlib.dl
Exception has been thrown by the target of an invocation.

i have a set of base class and derived class in which a protected
virtual function is being overridden by the derived class. This
function is called internally by the constructor of the base class and
the constructor is NOT being overridden by the derived class.

Anyone have any idea what am i doing wrong? can i invoke a virtual
function from inside a constructor?

Jaspinder

Dec 20 '05 #1
1 1708

JP wrote:
Hi,

I am facing a strange problem, please take a look at the code below:
It really helps us to help you if you post real code. Below is some
real code which is the result of me attempting to deduce what you
meant:

using System;

namespace ConsoleApplication4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SaveTree t = new SaveSingleTree();

Console.ReadLine();
}
}

public class SaveTree
{
public SaveTree()
{
//some code
Initranges();
}

protected virtual void Initranges()
{
//some code
}
}

public class SaveSingleTree: SaveTree
{
//constructor not being overridden
protected override void Initranges()
{
}
}
}
Now, when i do something like this:

SaveTree tree = new SaveSingleTree()

I get an exception with the following text:
'System.Reflection.TargetInvocationException' occurred in mscorlib.dl
Exception has been thrown by the target of an invocation.

i have a set of base class and derived class in which a protected
virtual function is being overridden by the derived class. This
function is called internally by the constructor of the base class and
the constructor is NOT being overridden by the derived class.
The code above matches this description and performs without error or
exception.

Anyone have any idea what am i doing wrong? can i invoke a virtual
function from inside a constructor?


Show us your code.

--
Larry Lard
Replies to group please

Dec 20 '05 #2

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

Similar topics

6
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
22
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
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 {
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
3
by: Biswajit Jena | last post by:
Is it possible to call a member function in constructor of the same class ? how ? I have a templated member function which I want to call in the constructor of the same class. How will I do...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
0
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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,...

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.