I recently expressed my view about this elsewhere* , but I think it deserves further analysis so I'm posting this as its own question.
Let's say that I need to create and pass around a container in my program. I probably don't have a strong opinion about one kind of container versus another, at least at this stage, but I do pick one; fo...
Hello,
Assume we have legacy classes, that can't be modified:
class Foo
{
public void Calculate(int a) { }
}
class Bar
{
public void Compute(int a) {}
}
I want to write a helper with such signature:
void Calc(object obj, int a);
Notice, that the first argument is of type 'object'. The test code should be some like this:
...
What is the relationship between using virtual functions and C++ inheritance mechanisms versus using templates and something like boost concepts?
It seems like there is quite an overlap of what is possible. Namely, it appears to be possible to achieve polymorphic behavior with either approach. So, when does it make sense to favor one ov...
I have two parallel inheritance chains:
Chain1:
Animal <- Lion
<- Gazelle
Chain2:
Food <- Meat
<- Grass
I want to implement the "Eats" polymorphic property on Animal. This is how it looks like:
public abstract class Animal
{
public abstract Food Eats { get; set;}
}
public class Lion : Animal
{
public override F...
I have a customer hierarchy like so:
abstract class Customer {
public virtual string Name { get; set; }
}
class HighValueCustomer : Customer {
public virtual int MaxSpending { get; set; }
}
class SpecialCustomer : Customer {
public virtual string Award { get; set; }
}
When I retrieve a Customer, I would like to show on ...
In C# what does the term shadowing mean? I have read this link but didn't fully understand it.
...
I have the following structure:
abstract class Base {
public abstract List<...> Get(); //What should be the generic type?
}
class SubOne : Base {
public override List<SubOne> Get() {
}
}
class SubTwo : Base {
public override List<SubTwo> Get() {
}
}
I want to create an abstract method that returns whate...
I seem to recall reading somewhere that the cost of a virtual call in C# is not as high, relatively speaking, as in C++. Is this true? If so - why?
...
Hi,
I'm looking at building a WCF service that can store/retrieve a range of different types.
Is the following example workable and also considered acceptable design:
[ServiceContract]
public interface IConnection
{
[OperationContract]
IObject RetrieveObject(Guid ObjectID);
[OperationContract]
Guid StoreObject(I...
What is the best way to implement polymorphic behavior in classes that I can't modify? I currently have some code like:
if(obj is ClassA) {
// ...
} else if(obj is ClassB) {
// ...
} else if ...
The obvious answer is to add a virtual method to the base class, but unfortunately the code is in a different assembly and I can't m...
Is there a way to print polymorphic values in Standard ML (SML/NJ specifically)? I have a polymorphic function that is not doing what I want and due to the abysmal state that is debugging in SML (see Any real world experience debugging a production functional program?), I would like to see what it is doing with some good-ol' print's. A...
I'm writing a module and want to have a unified exception hierarchy for the exceptions that it can raise. This allows users of the module to catch those particular exceptions and handle them distinctly, if needed. But many of the exceptions raised from the module are raised because of some other exception; e.g. failing at some task becau...
Hi,
I'd like to construct a message with unknown length or number of arguments. I took a simple template like
template <typename T> class Argument {
public:
int size;
int type;
T data;
};
and with some overloaded
addMessage (int value) {
Argument<int> *a = new Argument<int>;
vec.push_back(a);
}
(same for string and so ...
Hello,
I have a problem (it's my fault, I just can't spot what I'm doing wrong) where "ToString" isn't calling the correct method...
public class ClassA
{
public override ToString()
{
return "Hello, I'm class A.";
}
}
public class ClassB : ClassA
{
public override ToString()
{
return "Hello, I'm class B.";
...
I'm still learning C++; I was trying out how polymorphism works and I got a segmentation fault when calling a virtual method.
(Note: I didn't mark the destructor as virtual, I was just trying out to see what happens.) Here's the code:
#include <iostream>
using namespace std;
class Base
{
protected:
char *name;
public:
Base(char ...
In fairly large Ruby application, we have a situation where a given object is identified by a couple of things: name and id, say. Each of these value types serves a somewhat different purpose and so are not exactly equivalent (id and name persist in different places). So we wind-up with a variety of values being passed around the applica...
Hello, I have problem with class design.
I have core class for my game objects. While drawing I need to retrieve position from the object. Unfortunately object is seen as it's base type so the position is not retrieved from derived class but from it's parent. Defining field as virtual would fix my problem, but it's impossible :(. How ca...
Hello, This time I have problem with virtual fields.
I have core class for my game objects. This class contains a field with Model class object. Model's object contains values such as position etc.
Now - while drawing I need to read position of each object from it's model. The problem starts when instead of default model class I'm usin...
Hello, This time I have problem with virtual fields.
I have core class for my game objects. This class contains a field with Model class object. Model's object contains values such as position etc.
Now - while drawing I need to read position of each object from it's model. The problem starts when instead of default model class I'm usin...
I have the following classes:
class foo {
public void a() {
print("a");
}
public void b() {
a();
}
}
class bar extends foo {
public void a() {
print("overwritten a");
}
}
When I now call bar.b() I want it call the overwritten method a() in foo. It does, however, print "a".
...